Warning
This feature requires the API listener to work.
This listener appends pagination information to the API responses that is contain pagination information.
Attach this listener to your AppController components array if you want to make it available for all your controllers, application wide.
<?php
class AppController extends \Cake\Controller\Controller {
public function initialize(): void
{
$this->loadComponent('RequestHandler');
$this->loadComponent('Crud.Crud', [
'listeners' => [
'Crud.Api', // Required
'Crud.ApiPagination'
]
]);
}
}
Attach it on the fly in your controller beforeFilter if you want to limit availability of the listener to specific controllers and actions.
<?php
class SamplesController extends AppController
{
public function beforeFilter(\Cake\Event\EventInterface $event)
{
$this->Crud->addListener('Crud.Api'); // Required
$this->Crud->addListener('Crud.ApiPagination');
}
}
Paginated results will include a new pagination element similar to the one below:
{
"success": true,
"data":[
],
"pagination":{
"page_count": 13,
"current_page": 1,
"count": 25,
"has_prev_page": false,
"has_next_page": true
}
}
Configure this listener by setting the CakePHP Pagination options directly to the query object.
public function index()
{
$this->Crud->on('beforePaginate', function (\Cake\Event\EventInterface $event) {
$event->getSubject()->query->contain([
'Comments' => function ($q) {
return $q
->select(['id', 'name', 'description'])
->where([
'Comments.approved' => true
]);
}
]);
});
}