From 63240b68d5d42b9d36e07ef0b41fdb6f3ae310a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Sun, 16 Feb 2014 19:09:20 +0100 Subject: [PATCH 1/7] Don't pass the metadata to actions anymore --- .../Mvc/Controller/MethodHandler/DeleteHandler.php | 2 +- .../Mvc/Controller/MethodHandler/GetHandler.php | 2 +- .../Mvc/Controller/MethodHandler/PostHandler.php | 2 +- .../Mvc/Controller/MethodHandler/PutHandler.php | 2 +- .../Controller/MethodHandler/DeleteHandlerTest.php | 14 ++------------ .../Controller/MethodHandler/GetHandlerTest.php | 7 +------ 6 files changed, 7 insertions(+), 22 deletions(-) diff --git a/src/ZfrRest/Mvc/Controller/MethodHandler/DeleteHandler.php b/src/ZfrRest/Mvc/Controller/MethodHandler/DeleteHandler.php index f82c03f..693baff 100644 --- a/src/ZfrRest/Mvc/Controller/MethodHandler/DeleteHandler.php +++ b/src/ZfrRest/Mvc/Controller/MethodHandler/DeleteHandler.php @@ -47,7 +47,7 @@ public function handleMethod(AbstractController $controller, ResourceInterface $ throw new MethodNotAllowedException(); } - $result = $controller->delete($resource->getData(), $resource->getMetadata()); + $result = $controller->delete($resource->getData()); // According to http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7, status code should // be 204 if nothing is returned diff --git a/src/ZfrRest/Mvc/Controller/MethodHandler/GetHandler.php b/src/ZfrRest/Mvc/Controller/MethodHandler/GetHandler.php index 8c531f0..43240bc 100644 --- a/src/ZfrRest/Mvc/Controller/MethodHandler/GetHandler.php +++ b/src/ZfrRest/Mvc/Controller/MethodHandler/GetHandler.php @@ -50,6 +50,6 @@ public function handleMethod(AbstractController $controller, ResourceInterface $ throw new MethodNotAllowedException(); } - return $controller->get($resource->getData(), $resource->getMetadata()); + return $controller->get($resource->getData()); } } diff --git a/src/ZfrRest/Mvc/Controller/MethodHandler/PostHandler.php b/src/ZfrRest/Mvc/Controller/MethodHandler/PostHandler.php index b2a426d..6d1a04f 100644 --- a/src/ZfrRest/Mvc/Controller/MethodHandler/PostHandler.php +++ b/src/ZfrRest/Mvc/Controller/MethodHandler/PostHandler.php @@ -97,7 +97,7 @@ public function handleMethod(AbstractController $controller, ResourceInterface $ $data = $this->validateData($singleResource, $data); $data = $this->hydrateData($singleResource, $data); - $result = $controller->post($data, $singleResource->getMetadata()); + $result = $controller->post($data); // Set the Location header with the URL of the newly created resource if ($result instanceof ResourceModel) { diff --git a/src/ZfrRest/Mvc/Controller/MethodHandler/PutHandler.php b/src/ZfrRest/Mvc/Controller/MethodHandler/PutHandler.php index 933e43d..2a813da 100644 --- a/src/ZfrRest/Mvc/Controller/MethodHandler/PutHandler.php +++ b/src/ZfrRest/Mvc/Controller/MethodHandler/PutHandler.php @@ -90,7 +90,7 @@ public function handleMethod(AbstractController $controller, ResourceInterface $ $data = $this->validateData($resource, $data); $data = $this->hydrateData($resource, $data); - return $controller->put($data, $resource->getMetadata()); + return $controller->put($data); } /** diff --git a/tests/ZfrRestTest/Mvc/Controller/MethodHandler/DeleteHandlerTest.php b/tests/ZfrRestTest/Mvc/Controller/MethodHandler/DeleteHandlerTest.php index b9c265e..8d1b1ab 100644 --- a/tests/ZfrRestTest/Mvc/Controller/MethodHandler/DeleteHandlerTest.php +++ b/tests/ZfrRestTest/Mvc/Controller/MethodHandler/DeleteHandlerTest.php @@ -51,14 +51,9 @@ public function testCanReturnData() ->method('getData') ->will($this->returnValue($data)); - $metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface'); - $resource->expects($this->once()) - ->method('getMetadata') - ->will($this->returnValue($metadata)); - $controller->expects($this->once()) ->method('delete') - ->with($data, $metadata) + ->with($data) ->will($this->returnValue(['foo' => 'bar'])); $controller->expects($this->never()) @@ -84,14 +79,9 @@ public function testSetProperStatusCodeIfNothingIsReturnedFromDeleteMethod() ->method('getData') ->will($this->returnValue($data)); - $metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface'); - $resource->expects($this->once()) - ->method('getMetadata') - ->will($this->returnValue($metadata)); - $controller->expects($this->once()) ->method('delete') - ->with($data, $metadata) + ->with($data) ->will($this->returnValue(null)); $response = new HttpResponse(); diff --git a/tests/ZfrRestTest/Mvc/Controller/MethodHandler/GetHandlerTest.php b/tests/ZfrRestTest/Mvc/Controller/MethodHandler/GetHandlerTest.php index de6e8a2..e8f3471 100644 --- a/tests/ZfrRestTest/Mvc/Controller/MethodHandler/GetHandlerTest.php +++ b/tests/ZfrRestTest/Mvc/Controller/MethodHandler/GetHandlerTest.php @@ -50,14 +50,9 @@ public function testCanReturnData() ->method('getData') ->will($this->returnValue($data)); - $metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface'); - $resource->expects($this->once()) - ->method('getMetadata') - ->will($this->returnValue($metadata)); - $controller->expects($this->once()) ->method('get') - ->with($data, $metadata) + ->with($data) ->will($this->returnValue(['foo' => 'bar'])); $controller->expects($this->never()) From 15b2f0ea4327c0764aec33b88e313055982dca67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Sun, 16 Feb 2014 19:14:40 +0100 Subject: [PATCH 2/7] Allow to retrieve the matched resource from controller --- .../Mvc/Controller/AbstractRestfulController.php | 16 +++++++++++----- .../Controller/AbstractRestfulControllerTest.php | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php b/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php index 442098a..42fc9a9 100755 --- a/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php +++ b/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php @@ -26,11 +26,12 @@ use ZfrRest\Http\Exception\Client\NotFoundException; use ZfrRest\Mvc\Controller\MethodHandler\MethodHandlerPluginManager; use ZfrRest\Mvc\Exception\RuntimeException; +use ZfrRest\Resource\ResourceInterface; /** * @author Michaël Gallego * @licence MIT - * + * * @method \Zend\Paginator\Paginator paginatorWrapper(\Doctrine\Common\Collections\Collection $data, $criteria = []) */ class AbstractRestfulController extends AbstractController @@ -64,11 +65,8 @@ public function onDispatch(MvcEvent $event) $request = $this->getRequest(); $handler = $this->getMethodHandlerManager()->get($request->getMethod()); - /* @var \ZfrRest\Resource\ResourceInterface $resource */ - $resource = $event->getRouteMatch()->getParam('resource', null); - // We should always have a resource, otherwise throw an 404 exception - if (null === $resource) { + if (!$resource = $this->getMatchedResource()) { throw new NotFoundException(); } @@ -78,6 +76,14 @@ public function onDispatch(MvcEvent $event) return $result; } + /** + * @return ResourceInterface + */ + public function getMatchedResource() + { + return $this->getEvent()->getRouteMatch()->getParam('resource', null); + } + /** * Get the method handler plugin manager * diff --git a/tests/ZfrRestTest/Mvc/Controller/AbstractRestfulControllerTest.php b/tests/ZfrRestTest/Mvc/Controller/AbstractRestfulControllerTest.php index 9a57c45..c24f6e6 100644 --- a/tests/ZfrRestTest/Mvc/Controller/AbstractRestfulControllerTest.php +++ b/tests/ZfrRestTest/Mvc/Controller/AbstractRestfulControllerTest.php @@ -49,6 +49,7 @@ public function testThrowNotFoundExceptionIfNoResourceIsMatched() $event->setRouteMatch($routeMatch); $controller = new AbstractRestfulController(); + $controller->setEvent($event); $serviceLocator = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); $pluginManager = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); @@ -76,6 +77,7 @@ public function testCanSetResultIfResource() $pluginManager = $this->getMock('Zend\ServiceManager\ServiceLocatorInterface'); $controller->setServiceLocator($serviceLocator); + $controller->setEvent($event); $serviceLocator->expects($this->once()) ->method('get') From aba6c0776c1d4bede7c654d9d0080fde03de1eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Sun, 16 Feb 2014 19:49:05 +0100 Subject: [PATCH 3/7] Add ResourceModel --- config/module.config.php | 3 +- .../Mvc/Controller/Plugin/ResourceModel.php | 60 ++++++++++++++++++ .../Plugin/PaginatorWrapperTest.php | 2 +- .../Controller/Plugin/ResourceModelTest.php | 61 +++++++++++++++++++ 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php create mode 100644 tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php diff --git a/config/module.config.php b/config/module.config.php index af41ff5..2af0011 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -51,7 +51,8 @@ 'controller_plugins' => [ 'invokables' => [ - 'paginatorWrapper' => 'ZfrRest\Mvc\Controller\Plugin\PaginatorWrapper' + 'paginatorWrapper' => 'ZfrRest\Mvc\Controller\Plugin\PaginatorWrapper', + 'resourceModel' => 'ZfrRest\Mvc\Controller\Plugin\ResourceModel' ] ], diff --git a/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php b/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php new file mode 100644 index 0000000..d8ab525 --- /dev/null +++ b/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php @@ -0,0 +1,60 @@ + + * @licence MIT + */ +class ResourceModel extends AbstractPlugin +{ + /** + * Create a resource model from the data + * + * If no resource metadata interface is passed, it will fetched it from matched resource of + * the controller (which is what you want 99% of the time). + * + * @param mixed $data + * @param ResourceMetadataInterface|null $resourceMetadata + * @return ViewResourceModel + * @throws RuntimeException + */ + public function __invoke($data, ResourceMetadataInterface $resourceMetadata = null) + { + if (!$this->controller instanceof AbstractRestfulController) { + throw new RuntimeException( + 'You tried to use the ResourceModel controller plugin on a controller instance that does + not extend "ZfrRest\Mvc\Controller\AbstractRestfulController"' + ); + } + + $resourceMetadata = $resourceMetadata ?: $this->controller->getMatchedResource()->getMetadata(); + + return new ViewResourceModel(new Resource($data, $resourceMetadata)); + } +} diff --git a/tests/ZfrRestTest/Mvc/Controller/Plugin/PaginatorWrapperTest.php b/tests/ZfrRestTest/Mvc/Controller/Plugin/PaginatorWrapperTest.php index 6baadff..f782fa8 100644 --- a/tests/ZfrRestTest/Mvc/Controller/Plugin/PaginatorWrapperTest.php +++ b/tests/ZfrRestTest/Mvc/Controller/Plugin/PaginatorWrapperTest.php @@ -28,7 +28,7 @@ * @group Coverage * @covers \ZfrRest\Mvc\Controller\Plugin\PaginatorWrapper */ -class AbstractRestfulControllerTest extends PHPUnit_Framework_TestCase +class PaginatorWrapperTest extends PHPUnit_Framework_TestCase { public function testCanCreatePaginatorFromCollection() { diff --git a/tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php b/tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php new file mode 100644 index 0000000..814e5f2 --- /dev/null +++ b/tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php @@ -0,0 +1,61 @@ + + * + * @group Coverage + * @covers \ZfrRest\Mvc\Controller\Plugin\ResourceModel + */ +class ResourceModelTest extends PHPUnit_Framework_TestCase +{ + public function testThrowExceptionIfNotAbstractRestfulController() + { + $this->setExpectedException('ZfrRest\Mvc\Exception\RuntimeException'); + + $plugin = new ResourceModel(); + $plugin(new \stdClass()); + } + + public function testCanCreateResourceModelWithMetadata() + { + $controller = new AbstractRestfulController(); + $plugin = new ResourceModel(); + $plugin->setController($controller); + + $reflClass = $this->getMock('ReflectionClass', [], [], '', false); + $reflClass->expects($this->once())->method('isInstance')->will($this->returnValue(true)); + + $metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface'); + $metadata->expects($this->once())->method('getReflectionClass')->will($this->returnValue($reflClass)); + + $data = new \stdClass(); + $resourceModel = $plugin($data, $metadata); + + $this->assertInstanceOf('ZfrRest\View\Model\ResourceModel', $resourceModel); + $this->assertSame($data, $resourceModel->getResource()->getData()); + $this->assertSame($metadata, $resourceModel->getResource()->getMetadata()); + } +} From 9b2fd1c5da287425ba3c0bcf976b542011dd37bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Sun, 16 Feb 2014 19:59:54 +0100 Subject: [PATCH 4/7] Add docblock --- src/ZfrRest/Mvc/Controller/AbstractRestfulController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php b/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php index 42fc9a9..5d5265f 100755 --- a/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php +++ b/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php @@ -33,6 +33,7 @@ * @licence MIT * * @method \Zend\Paginator\Paginator paginatorWrapper(\Doctrine\Common\Collections\Collection $data, $criteria = []) + * @method \ZfrRest\View\Model\ResourceModel resourceModel($data, \ZfrRest\Resource\Metadata\ResourceMetadataInterface $metadata = null) */ class AbstractRestfulController extends AbstractController { From ca13fa9baf0a6a298b9173fa682c69ed5fc9707b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Mon, 17 Feb 2014 14:10:05 +0100 Subject: [PATCH 5/7] Fix naming and add imports to avoid CS limit --- .../Mvc/Controller/AbstractRestfulController.php | 10 +++++++--- src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php b/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php index 5d5265f..9ce2bfc 100755 --- a/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php +++ b/src/ZfrRest/Mvc/Controller/AbstractRestfulController.php @@ -18,22 +18,26 @@ namespace ZfrRest\Mvc\Controller; +use Doctrine\Common\Collections\Collection; use Zend\Http\Request as HttpRequest; use Zend\Mvc\Controller\AbstractController; use Zend\Mvc\MvcEvent; +use Zend\Paginator\Paginator; use Zend\Stdlib\RequestInterface; use Zend\Stdlib\ResponseInterface; use ZfrRest\Http\Exception\Client\NotFoundException; use ZfrRest\Mvc\Controller\MethodHandler\MethodHandlerPluginManager; use ZfrRest\Mvc\Exception\RuntimeException; use ZfrRest\Resource\ResourceInterface; +use ZfrRest\Resource\Metadata\ResourceMetadataInterface; +use ZfrRest\View\Model\ResourceModel; /** * @author Michaël Gallego * @licence MIT * - * @method \Zend\Paginator\Paginator paginatorWrapper(\Doctrine\Common\Collections\Collection $data, $criteria = []) - * @method \ZfrRest\View\Model\ResourceModel resourceModel($data, \ZfrRest\Resource\Metadata\ResourceMetadataInterface $metadata = null) + * @method Paginator paginatorWrapper(Collection $data, $criteria = []) + * @method ResourceModel resourceModel($data, ResourceMetadataInterface $metadata = null) */ class AbstractRestfulController extends AbstractController { @@ -78,7 +82,7 @@ public function onDispatch(MvcEvent $event) } /** - * @return ResourceInterface + * @return ResourceInterface|null */ public function getMatchedResource() { diff --git a/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php b/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php index d8ab525..4f61beb 100644 --- a/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php +++ b/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php @@ -23,7 +23,7 @@ use ZfrRest\Mvc\Exception\RuntimeException; use ZfrRest\Resource\Metadata\ResourceMetadataInterface; use ZfrRest\Resource\Resource; -use ZfrRest\View\Model\ResourceModel as ViewResourceModel; +use ZfrRest\View\Model\ResourceModel as ResourceViewModel; /** * Controller plugin that allows to create a resource model quickly. @@ -41,7 +41,7 @@ class ResourceModel extends AbstractPlugin * * @param mixed $data * @param ResourceMetadataInterface|null $resourceMetadata - * @return ViewResourceModel + * @return ResourceViewModel * @throws RuntimeException */ public function __invoke($data, ResourceMetadataInterface $resourceMetadata = null) @@ -55,6 +55,6 @@ public function __invoke($data, ResourceMetadataInterface $resourceMetadata = nu $resourceMetadata = $resourceMetadata ?: $this->controller->getMatchedResource()->getMetadata(); - return new ViewResourceModel(new Resource($data, $resourceMetadata)); + return new ResourceViewModel(new Resource($data, $resourceMetadata)); } } From 2368b97ec7aaa0da4b505f3c098409d1379da908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Mon, 17 Feb 2014 14:16:01 +0100 Subject: [PATCH 6/7] Update doc --- docs/02. Quick Start.md | 47 +++++++++++++++++++++++------------------ docs/07. Cookbook.md | 6 +++--- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/docs/02. Quick Start.md b/docs/02. Quick Start.md index aa59597..0b9e011 100644 --- a/docs/02. Quick Start.md +++ b/docs/02. Quick Start.md @@ -212,23 +212,23 @@ class UserController extends AbstractRestfulController */ protected $userService; - public function delete(User $user, ResourceMetadataInterface $metadata) + public function delete(User $user) { $this->userService->delete($user); } - public function put(User $user, ResourceMetadataInterface $metadata) + public function put(User $user) { $this->userService->update($user); - return $user; + return $this->resourceModel($user); } - public function get(User $user, ResourceMetadataInterface $metadata) + public function get(User $user) { // Do things if you want - return $user; + return $this->resourceModel($user); } } ``` @@ -239,16 +239,17 @@ you from using a normal ZF2 controller for some actions). As you can guess, each "action" is named after the corresponding HTTP method. ZfrRest is flexible enough that you can add custom HTTP verbs by adding specific method handlers and more public methods in your controller. -The interesting thing is that, contrary to traditional ZF2 controllers, you receive parameters in each method: the -actual resource (in this case, a User) as well as the resource metadata for the resource that has been matched by -the router. You have nothing to do, this is done automatically by ZfrRest, so that your controller stays really clean. -The only thing you need to do is **passing the resource to the service for your business logic**. +The interesting thing is that, contrary to traditional ZF2 controllers, you receive parameter in each method: the +actual resource data (in this case, a User) for the resource that have been matched by the router. You have nothing +to do, this is done automatically by ZfrRest, so that your controller stays really clean. The only thing you need +to do is **passing the resource to the service for your business logic**. For the `put` method, you don't even need to manually validate the user data, because it has already been validated using the input filter you specified in the mapping. If data would have been invalid, it would have returned a 400 Bad Request error, with the various error messages under the `errors` key. -If you want automatic serialization of the resource, you must return a ResourceModel (it needs a resource). +If you want automatic serialization of the resource, you must return a ResourceModel (it needs a resource). ZfrRest +provides you a utility controller plugin called `resourceModel` that you can use to create one. Now, let's see the `Application\Controller\UsersController` controller: @@ -258,7 +259,7 @@ Now, let's see the `Application\Controller\UsersController` controller: namespace Application\Controller; use Application\Entity\User; -use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\Selectable; use DoctrineModule\Paginator\Adapter\Collection as CollectionAdapter; use Zend\Paginator\Paginator; use ZfrRest\Mvc\Controller\AbstractRestfulController; @@ -270,14 +271,14 @@ class UsersController extends AbstractRestfulController */ protected $userService; - public function post(User $user, ResourceMetadataInterface $metadata) + public function post(User $user) { $this->userService->create($user); - return new ResourceModel(new Resource($user, $metadata)); + return $this->resourceModel($user); } - public function get(Collection $users, ResourceMetadataInterface $metadata) + public function get(Selectable $users) { // We can do filtering here on the collection, for example using the Doctrine API criteria @@ -285,7 +286,7 @@ class UsersController extends AbstractRestfulController $paginator = $this->paginatorWrapper($users); $paginator->setCurrentPageNumber($this->params()->fromQuery('page', 1)); - return new ResourceModel(new Resource($user, $metadata)); + return $this->resourceModel($paginator); } } ``` @@ -293,12 +294,16 @@ class UsersController extends AbstractRestfulController For the `post` method, it is exactly the same as the `put` method: you don't need to validate anything, because it has already been done for you. Just pass the entity to your service and you're done! -For the `get` method, we receive a Collection. Actually, if you are using Doctrine ORM, you can typehint to +For the `get` method, we receive a Selectable. Actually, if you are using Doctrine ORM, you can typehint to `Doctrine\Common\Collections\Selectable`, which is a much more useful interface because it allows to do efficient filtering. -As you can see from the example, we can also wrap the data around a Zend paginator, and directly return the -paginator. The `paginatorWrapper` is a simple controller plugin that encapsulate the logic of paginator creation. +> Most of the time, an object that implements the `Selectable` interface also implement `Collection` interface. However, +the `Selectable` interface is much more flexible as it has powerful filtering capabilities. That's why you should +always typehint to `Selectable` if you are using Doctrine ORM or Doctrine ODM. + +As you can see from the example, we can also wrap the data around a Zend paginator, and then wrap it around +a `ResourceModel`. The `paginatorWrapper` is a simple controller plugin that encapsulate the logic of paginator creation. You can also optionally pass it a criteria to filter the collection (see the cookbook). When you return a Paginator, ZfrRest will intelligently serialize the output. For instance, here is an example @@ -468,14 +473,14 @@ defined a controller `Application\Controller\TweetsController`. We therefore nee namespace Application\Controller; -use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\Selectable; use ZfrRest\Mvc\Controller\AbstractRestfulController; class TweetsController extends AbstractRestfulController { - public function get(Collection $tweets) + public function get(Selectable $tweets) { - return $tweets; + return $this->resourceModel($tweets); } } ``` diff --git a/docs/07. Cookbook.md b/docs/07. Cookbook.md index b5c5700..93760a7 100644 --- a/docs/07. Cookbook.md +++ b/docs/07. Cookbook.md @@ -78,7 +78,7 @@ public function get(Selectable $users) $paginator = $this->paginatorWrapper($users); $paginator->setCurrentPageNumber($this->params()->fromQuery('page', 1)); - return $paginator; + return $this->resourceModel($users); } ``` @@ -101,7 +101,7 @@ public function get(Selectable $users) $paginator = $this->paginatorWrapper($users, $criteria); $paginator->setCurrentPageNumber($this->params()->fromQuery('page', 1)); - return $paginator; + return $this->resourceModel($users); } ``` @@ -123,7 +123,7 @@ public function get(Selectable $users) $paginator = $this->paginatorWrapper($users, $criteria); $paginator->setCurrentPageNumber($this->params()->fromQuery('page', 1)); - return $paginator; + return $this->resourceModel($users); } ``` From 36cc1d3111afe36e1906c9439fb1422ec3dceff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Mon, 17 Feb 2014 14:41:01 +0100 Subject: [PATCH 7/7] Fix cookbook --- docs/07. Cookbook.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/07. Cookbook.md b/docs/07. Cookbook.md index 93760a7..27254cf 100644 --- a/docs/07. Cookbook.md +++ b/docs/07. Cookbook.md @@ -78,7 +78,7 @@ public function get(Selectable $users) $paginator = $this->paginatorWrapper($users); $paginator->setCurrentPageNumber($this->params()->fromQuery('page', 1)); - return $this->resourceModel($users); + return $this->resourceModel($paginator); } ``` @@ -101,7 +101,7 @@ public function get(Selectable $users) $paginator = $this->paginatorWrapper($users, $criteria); $paginator->setCurrentPageNumber($this->params()->fromQuery('page', 1)); - return $this->resourceModel($users); + return $this->resourceModel($paginator); } ``` @@ -123,7 +123,7 @@ public function get(Selectable $users) $paginator = $this->paginatorWrapper($users, $criteria); $paginator->setCurrentPageNumber($this->params()->fromQuery('page', 1)); - return $this->resourceModel($users); + return $this->resourceModel($paginator); } ```