GithubHelp home page GithubHelp logo

bcrowe / cakephp-api-pagination Goto Github PK

View Code? Open in Web Editor NEW
40.0 5.0 13.0 67 KB

:bookmark_tabs: CakePHP 4 plugin that injects pagination information into API responses.

License: MIT License

PHP 100.00%
cakephp api pagination php cakephp3 cakephp-plugin cakephp4

cakephp-api-pagination's Introduction

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.2+ which injects pagination information from CakePHP's Paginator into serialized JsonView and XmlView responses.

See 1.x and 2.x releases and branches of this plugin for support of previous versions of CakePHP before 4.2.

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.

cakephp-api-pagination's People

Contributors

bcrowe avatar ishanvyas22 avatar jfalbel avatar lukec8 avatar mtak3 avatar pabloelcolombiano avatar styks1987 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cakephp-api-pagination's Issues

Handle non conventional controller names

The name of the model is not settable and taken as the name of the controller performing the pagination.

This is a problem when performing the pagination on a controller that does not follow the CakePHP conventions, e.g. ArticlesIndexController.

This is addressed by the PR #7 .

API not getting response Data object

Hi there,
I am using this plugin for CakePHP 4 but I am not getting the data, its only giving pagination object.
my function is as follows:-
class ArticlesController extends AppController
{
/**
* Index method
*
*/
public function index()
{
$this->set('articles', $this->paginate($this->Articles));
$this->set('_serialize', ['articles']);
}
}

and its giving the output:-
{
"pagination": {
"count": 2,
"current": 2,
"perPage": 20,
"page": 1,
"requestedPage": 1,
"pageCount": 1,
"start": 1,
"end": 2,
"prevPage": false,
"nextPage": false,
"sort": null,
"direction": null,
"sortDefault": false,
"directionDefault": false,
"completeSort": [],
"limit": null,
"scope": null,
"finder": "all"
}
}
if I remove $this->paginate() method and use $this->set('articles', $this->Articles->find("all")->toArray()); then output comes:-
{
"articles": [
{
"id": 1,
"user_id": 1,
"title": "First Post",
"slug": "first-post",
"body": "This is the first post.",
"published": true,
"created": "2021-04-29T15:51:26+05:30",
"modified": "2021-04-29T15:51:26+05:30"
},
{
"id": 2,
"user_id": 2,
"title": "Test Category",
"slug": "testing",
"body": "Hello Body",
"published": false,
"created": "2021-04-29T11:13:06+05:30",
"modified": "2021-04-29T11:23:28+05:30"
}
]
}
I have tried everything but articles data not coming only pagination is coming.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.