All Projects β†’ bcrowe β†’ cakephp-api-pagination

bcrowe / cakephp-api-pagination

Licence: MIT license
πŸ“‘ CakePHP 4 plugin that injects pagination information into API responses.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to cakephp-api-pagination

auth
Auth objects for CakePHP
Stars: ✭ 28 (-28.21%)
Mutual labels:  cakephp, cakephp3, cakephp-plugin
Enum
Enumeration list for CakePHP 3
Stars: ✭ 27 (-30.77%)
Mutual labels:  cakephp, cakephp3, cakephp-plugin
cakephp-mailgun
Mailgun plugin for CakePHP 3
Stars: ✭ 23 (-41.03%)
Mutual labels:  cakephp, cakephp3, cakephp-plugin
cakephp-shim
CakePHP plugin to "shim" functionality up and down for major versions of the framework.
Stars: ✭ 37 (-5.13%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-feed
CakePHP Plugin with RssView to create RSS feeds.
Stars: ✭ 13 (-66.67%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-error-email
ErrorEmail Plugin for CakePHP3.x
Stars: ✭ 16 (-58.97%)
Mutual labels:  cakephp, cakephp-plugin
Search
CakePHP: Easy model searching
Stars: ✭ 153 (+292.31%)
Mutual labels:  cakephp, cakephp-plugin
mongodb-cakephp3
An Mongodb datasource for CakePHP 3.0
Stars: ✭ 29 (-25.64%)
Mutual labels:  cakephp, cakephp3
cakephp-ajax
AJAX for CakePHP: A plugin to ease handling AJAX requests.
Stars: ✭ 55 (+41.03%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-swagger-bake
Automatically generate OpenAPI, Swagger, and Redoc documentation from your existing CakePHP code.
Stars: ✭ 48 (+23.08%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-translate
A CakePHP plugin to manage translations of your static content the easy way via web backend.
Stars: ✭ 18 (-53.85%)
Mutual labels:  cakephp, cakephp-plugin
Orderly
Default ordering for your CakePHP tables
Stars: ✭ 21 (-46.15%)
Mutual labels:  cakephp, cakephp-plugin
asset-mix
Provides helpers functions for CakePHP to use Laravel Mix.
Stars: ✭ 27 (-30.77%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Csvview
CakePHP: A view class for generating CSV
Stars: ✭ 174 (+346.15%)
Mutual labels:  cakephp, cakephp-plugin
plum search
Plum Search plugin for CakePHP
Stars: ✭ 20 (-48.72%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Jwt Auth
A CakePHP plugin for authenticating using JSON Web Tokens
Stars: ✭ 153 (+292.31%)
Mutual labels:  cakephp, cakephp-plugin
cakephp-i18n
A CakePHP plugin with I18n related tools.
Stars: ✭ 40 (+2.56%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Ide Helper
IDE Helper plugin for CakePHP
Stars: ✭ 138 (+253.85%)
Mutual labels:  cakephp, cakephp-plugin
Cakephp Imagine Plugin
CakePHP wrapper for the powerful Imagine image processing library. Makes images manipulation easy and powerful.
Stars: ✭ 140 (+258.97%)
Mutual labels:  cakephp, cakephp-plugin
elastic-search
Elastic search datasource for CakePHP
Stars: ✭ 85 (+117.95%)
Mutual labels:  cakephp, cakephp-plugin

CakePHP API Pagination

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

This is a simple component for CakePHP 4 which injects pagination information from CakePHP's Paginator into serialized JsonView and XmlView responses.

Install

Via Composer

$ composer require bcrowe/cakephp-api-pagination

Load the plugin by adding $this->addPlugin('BryanCrowe/ApiPagination'); to the bootsrap method in your project’s src/Application.php:

public function bootstrap(): void
{
    parent::bootstrap();
    
    // ... bootstrap code ...

    // load more plugins here
    
    $this->addPlugin('BryanCrowe/ApiPagination');
}

Usage

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->viewBuilder()->setOption('serialize', ['articles']);

Note: It is important that your serialize option 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
    }
}

Configuring the Pagination Output

ApiPagination has four keys for configuration: key, aliases, visible and model.

  • key allows you to change the name of the pagination key.

  • aliases allows you to change names of the pagination detail keys.

  • visible allows 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.

  • model allows you to set the name of the model the pagination is applied on if the controller does not follow CakePHP conventions, e.g. ArticlesIndexController. Per default the model is the name of the controller, e.g. Articles for ArticlesController.

An example using all these configuration keys:

$this->loadComponent('BryanCrowe/ApiPagination.ApiPagination', [
    'key' => 'paging',
    'aliases' => [
        'page' => 'currentPage',
        'current' => 'resultCount'
    ],
    'visible' => [
        'currentPage',
        'resultCount',
        'prevPage',
        'nextPage'
    ],
    'model' => 'Articles',
]);

This configuration would yield:

{
    "articles": ["...", "...", "..."],
    "paging": {
        "prevPage": false,
        "nextPage": true,
        "currentPage": 1,
        "resultCount": 20
    }
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].