API Pagination

Warning

This feature requires the API listener to work.

This listener appends pagination information to the API responses that is contain pagination information.

Setup

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()
    {
        $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\Event $event)
    {
        $this->Crud->addListener('Crud.Api'); // Required
        $this->Crud->addListener('Crud.ApiPagination');
    }

}

Output

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
        }
}

Configuration

Configure this listener by setting the CakePHP Pagination options directly to the query object.

public function index()
{
    $this->Crud->on('beforePaginate', function (\Cake\Event\Event $event) {
        $event->getSubject()->query->contain([
            'Comments' => function ($q) {
                return $q
                    ->select(['id', 'name', 'description'])
                    ->where([
                        'Comments.approved' => true
                    ]);
            }
        ]);
    });
}
  v: 4.4.3
Versions
latest
stable
4.4.3
4.4.2
4.4.1
4.4.0
4.3.5
4.3.4
4.3.3
4.3.2
4.3.1
4.3.0
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.4
v4.0.0
cake3
Downloads
On Read the Docs
Project Home
Builds
Downloads
On GitHub
View
Edit

Free document hosting provided by Read the Docs.