diff --git a/src/Controller/Component/PaginatorComponent.php b/src/Controller/Component/PaginatorComponent.php index 82749a46f13..3d1e8bbada3 100644 --- a/src/Controller/Component/PaginatorComponent.php +++ b/src/Controller/Component/PaginatorComponent.php @@ -142,6 +142,22 @@ public function implementedEvents() * $results = $paginator->paginate($query); * ``` * + * ### Scoping Request parameters + * + * By using request parameter scopes you can paginate multiple queries in the same controller action: + * + * ``` + * $articles = $paginator->paginate($articlesQuery, ['scope' => 'articles']); + * $tags = $paginator->paginate($tagsQuery, ['scope' => 'tags']); + * ``` + * + * Each of the above queries will use different query string parameter sets + * for pagination data. An example URL paginating both results would be: + * + * ``` + * /dashboard?articles[page]=1&tags[page]=2 + * ``` + * * @param \Cake\Datasource\RepositoryInterface|\Cake\Datasource\QueryInterface $object The table or query to paginate. * @param array $settings The settings/configuration used for pagination. * @return \Cake\Datasource\ResultSetInterface Query results diff --git a/tests/TestCase/Controller/ControllerTest.php b/tests/TestCase/Controller/ControllerTest.php index bebef274ff2..b04a6c7858c 100644 --- a/tests/TestCase/Controller/ControllerTest.php +++ b/tests/TestCase/Controller/ControllerTest.php @@ -728,7 +728,7 @@ public function testPaginate() $Controller = new Controller($request, $response); $Controller->request->query = [ 'url' => [], - 'prefix' => [ + 'posts' => [ 'page' => 2, 'limit' => 2, ] @@ -751,8 +751,9 @@ public function testPaginate() $this->assertSame($Controller->request->params['paging']['Posts']['pageCount'], 1); $this->assertSame($Controller->request->params['paging']['Posts']['prevPage'], false); $this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false); + $this->assertNull($Controller->request->params['paging']['Posts']['scope']); - $results = $Controller->paginate(TableRegistry::get('Posts'), ['prefix' => 'prefix']); + $results = $Controller->paginate(TableRegistry::get('Posts'), ['scope' => 'posts']); $this->assertInstanceOf('Cake\Datasource\ResultSetInterface', $results); $this->assertCount(1, $results); @@ -760,6 +761,7 @@ public function testPaginate() $this->assertSame($Controller->request->params['paging']['Posts']['pageCount'], 2); $this->assertSame($Controller->request->params['paging']['Posts']['prevPage'], true); $this->assertSame($Controller->request->params['paging']['Posts']['nextPage'], false); + $this->assertSame($Controller->request->params['paging']['Posts']['scope'], 'posts'); } /**