Rapid pagination for CakePHP 2
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Model
Test
.gitignore
.php_cs
.scrutinizer.yml
.travis.yml
LICENSE
README.md
composer.json

README.md

lampager-cakephp2

Build Status Coverage Status Scrutinizer Code Quality

Lampager for CakePHP 2

Rapid pagination without using OFFSET

Requirements

Installing

composer require lampager/lampager-cakephp2

Move Plugin/Lampager to the appropriate directory if necessary.

Basic Usage

Load as a plugin. See How To Install Plugins - 2.x for detail.

Plugin needs to be loaded manually in app/Config/bootstrap.php:

// Be sure to require vendor/autoload.php beforehand.
// CakePlugin::load() will fail unless autoloader is properly configured.
CakePlugin::load('Lampager');

Next, add 'Lampager.Lampager' to your Model class (AppModel is preferable):

class AppModel extends Model
{
    public $actsAs = [
        'Lampager.Lampager',
    ];
}

Use in Controller

At first, your Model class must have 'Lampager.Lampager' enabled.

Accept cursor parameters from a request and pass it through PaginatorComponent::settings.

class PostsController extends AppController
{
    // Load default PaginatorComponent of CakePHP
    public $components = [
        'Paginator',
    ];

    public function index()
    {
        // Get cursor parameters
        $previous = $this->request->param('named.previous_cursor');
        $next = $this->request->param('named.next_cursor');

        $this->Paginator->settings = [
            // Lampager options
            // If the previous_cursor is not set, paginate forward; otherwise backward
            'forward' => !$previous,
            'cursor' => $previous ?: $next ?: [],
            'seekable' => true,

            // PaginatorComponent::settings query
            'conditions' => [
                'Post.type' => 'public',
            ],
            'order' => [
                'Post.created' => 'DESC',
                'Post.id' => 'DESC',
            ],
            'limit' => 15,
        ];

        /** @var mixed[][] */
        $posts = $this->Paginator->paginate(Post::class);
        $this->set('posts', $posts);
    }
}

And the pagination links can be output as follows:

// If there is a previous page, print pagination link
if ($posts->hasPrevious) {
    echo $this->Html->link('<< Previous', [
        'controller' => 'posts',
        'action' => 'index',
        'previous_cursor' => $posts->previousCursor,
    ]);
}

// If there is a next page, print pagination link
if ($posts->hasNext) {
    echo $this->Html->link('Next >>', [
        'controller' => 'posts',
        'action' => 'index',
        'next_cursor' => $posts->nextCursor,
    ]);
}

Use in Model

At first, your Model class must have 'Lampager.Lampager' enabled.

Simply use Model::find with lampager.

class Post extends AppModel
{
    /**
     * @return Lampager\PaginationResult
     */
    public function latest(array $cursor = [])
    {
        return $this->find('lampager', [
            // Lampager options
            'forward' => true,
            'seekable' => true,
            'cursor' => $cursor,

            // Model::find query
            'limit' => 10,
            'order' => [
                'Post.modified' => 'DESC',
                'Post.created' => 'DESC',
                'Post.id' => 'DESC',
            ],
        ]);
    }
}

Classes

See also: lampager/lampager.

Name Type Extends Description
LampagerBehavior Class ModelBehavior CakePHP behavior which handles Model::find() and PaginatorComponent::paginate()
LampagerArrayCursor Class Lampager\Contracts\Cursor Multi-dimensional array cursor
LampagerColumnAccess Class Multi-dimensional array accessor
LampagerTransformer Class CakePHP query genenrator
LampagerArrayProcessor Class Lampager\ArrayProcessor Processor implementation for CakePHP
LampagerPaginator Class Lampager\Paginator Paginator implementation for CakePHP

API

See also: lampager/lampager.

Using Model::find() or PaginatorComponent::paginate() is recommended. The query is merged with CakePHP query and passed to Lampager\Query.