This is a simple component for CakePHP 3 which injects pagination information from CakePHP's Paginator into serialized JsonView and XmlView responses.
Via Composer
$ composer require bcrowe/cakephp-api-paginationLoad the plugin in your application's bootstrap.php file:
Plugin::load('BryanCrowe/ApiPagination');Make sure your application has been set up to use data views; see the Enabling Data Views in Your Application section of the CakePHP documentation.
Then, load ApiPaginationComponent:
$this->loadComponent('BryanCrowe/ApiPagination.ApiPagination');Then, go ahead and set your paginated view variable like so:
$this->set('articles', $this->paginate($this->Articles));
$this->set('_serialize', ['articles']);Note: It is important that your _serialize variable is an array, e.g.
['articles'], so that your pagination information can be set under its own
pagination key.
Your JsonView and XmlView responses will now contain the pagination information, and will look something like this:
{
"articles": ["...", "...", "..."],
"pagination": {
"finder": "all",
"page": 1,
"current": 20,
"count": 5000,
"perPage": 20,
"prevPage": false,
"nextPage": true,
"pageCount": 250,
"sort": null,
"direction": false,
"limit": null,
"sortDefault": false,
"directionDefault": false
}
}ApiPagination has three keys for configuration: key, aliases, and visible.
-
keyallows you to change the name of the pagination key. -
aliasesallows you to change names of the pagination detail keys. -
visibleallows you to set which pagination keys will be exposed in the response. Note: Whenever setting a key's visibility, make sure to use the aliased name if you've given it one.
An example using all these configuration keys:
$this->loadComponent('BryanCrowe/ApiPagination.ApiPagination', [
'key' => 'paging',
'aliases' => [
'page' => 'currentPage',
'current' => 'resultCount'
],
'visible' => [
'currentPage',
'resultCount',
'prevPage',
'nextPage'
]
]);This configuration would yield:
{
"articles": ["...", "...", "..."],
"paging": {
"prevPage": false,
"nextPage": true,
"currentPage": 1,
"resultCount": 20
}
}Please see CHANGELOG for more information what has changed recently.
$ composer testPlease see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.