codag / restfabrication-bundle
Symfony2 Bundle,用于在FOSRestBundle之上快速开发RESTful API
Requires
- php: >=5.3.2
- doctrine/orm: ~2.2,>=2.2.3
- friendsofsymfony/rest-bundle: 1.4.*
- symfony/form: ~2.2
This package is not auto-updated.
Last update: 2024-09-24 02:23:57 UTC
README
此bundle提供了一种快速开发Symfony2项目RESTful API的方法。
安装
- 将CodagRestFabricationBundle添加到您的composer.json中
- 启用bundle
步骤1:将CodagRestFabricationBundle添加到您的composer.json
{ "require": { "codag/restfabrication-bundle": "dev-master" } }
更新项目依赖项
php composer.phar update codag/restfabrication-bundle
步骤2:启用bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new Codag\RestFabricationBundle\CodagRestFabricationBundle(), ); }
使用方法
有关更多实现示例,请参阅以下博客文章:(即将推出)。
领域管理器
领域管理器是一种更抽象的方式与数据层(例如Doctrine)通信。由于它接受一个实体作为参数,因此其优点是可重用性高。每个资源应由自己的领域管理器表示,这可以在服务容器(services.xml)中轻松定义。
因此,我们为所有资源(实体)创建新的服务,以便每次都会创建一个新的类"codag_rest_fabrication.domain_manager.default.class",并将其提供的实体作为参数实例化。
<service id="acme_api.domain_manager.myresource" class="%codag_rest_fabrication.domain_manager.default.class%"> <argument type="service" id="doctrine.orm.entity_manager" /> <argument>AcmeApiBundle:Myresource</argument> </service>
现在创建的领域管理器可以在控制器中使用,以避免为它们表示的简单RESTful(GET/DELETE)请求实现重复的代码。
public function getAction() { return $this->get('acme_api.domain_manager.myresource')->findAll(); }
public function deleteAction(Request $request, $id) { $manager = $this->get('acme_api.domain_manager.myresource'); $obj = $manager->find($id); if(!$obj){ throw new ResourceNotFoundException('Myresource', $id); } $manager->delete($id); return $this->routeRedirectView('myresource_all', array(), Codes::HTTP_NO_CONTENT); }
正如我们所看到的,领域管理器提供了查找一个或多个条目的方法,并可以直接删除条目。请参阅完整实现集的代码。
表单处理器
表单处理器依赖于领域管理器,并提供处理在PUT/POST请求过程中创建的表单的功能。
因此,我们为所有依赖表单的资源(实体)创建新的服务,以便每次都会创建一个新的类"codag_rest_fabrication.form_handler.create_form.class"。作为单个参数,必须提供相关的资源领域管理器。
<service id="acme_api.form_handler.myresource" class="%codag_rest_fabrication.form_handler.create_form.class%"> <argument type="service" id="acme_api.domain_manager.myresource" /> </service>
现在创建的表单处理器可以在控制器中使用,以避免为它们表示的简单RESTful(PUT/POST)请求和需要通过表单处理的方法实现重复的代码。
public function postAction(Request $request){ try { $form = $this->createForm(new MyresourceType(), new Myresource(), array('method' => 'POST')); $new = $this->get('acme_api.form_handler.myresource')->handle($form, $request); return $this->routeRedirectView('myresource_get', array('id' => $new->getId()), Codes::HTTP_CREATED); }catch (InvalidFormException $exception) { return $exception->getForm(); } }
public function putAction(Request $request, $id){ try { $manager = $this->get('acme_api.domain_manager.myresource'); $formHandler = $this->get('acme_api.form_handler.myresource'); if (!($object = $manager->get($id))) { $statusCode = Codes::HTTP_CREATED; $form = $this->createForm(new MyresourceType(), new Myresource(), array('method' => 'POST')); } else { $statusCode = Codes::HTTP_NO_CONTENT; $form = $this->createForm(new MyresourceType(), $object, array('method' => 'PUT')); } $object = $formHandler->handle($form, $reques�t); return $this->routeRedirectView('myresource_get_all', array('id' => $object->getId()), $statusCode); } catch (InvalidFormException $exception) { return $exception->getForm(); } }
异常
InvalidFormException
为了能够处理干净的错误管理,此异常可以在处理表单时使用。异常可以在表单处理器的过程中抛出。为了验证目的,异常将在控制器中被捕获,最后将表单返回到视图。
表单处理器
if($form->isValid()){ ... } throw new InvalidFormException('Invalid submitted data', $form);
控制器
try { ... } catch (InvalidFormException $exception) { return $exception->getForm(); }
ResourceNotFoundException
为了避免不断重复自己,此异常是NotFoundHttpException的包装器,其中包含标准句子。此外,它还包含资源名称以及标识符的值。
控制器
throw new ResourceNotFoundException('Myresource', $id);
输出
{
"code": 404,
"message": "Myresource not found with id: 123"
}
RestException
为了完整起见,此异常是HttpException的包装器,并且可能在不久的将来扩展。
##贡献
如果此bundle不允许您自定义选项,我邀请您分支项目,创建一个功能分支,并提交拉取请求。
为了确保代码库的一致性,您应该确保代码遵循编码标准。
##许可证
此软件包采用MIT许可证。请参阅完整的许可证此处。