PHP code example of lampager / lampager-cakephp2

1. Go to this page and download the library: Download lampager/lampager-cakephp2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lampager / lampager-cakephp2 example snippets


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

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

/** @var \Lampager\PaginationResult $posts */
$posts = $this->paginate(Post::class, [
    // Lampager options
    'forward' => true,
    'seekable' => true,
    'cursor' => [
        'Post' => [
            'id' => '4',
            'created' => '2017-01-01 10:00:00',
        ],
    ],

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

$this->set('posts', $posts);

/** @var \Lampager\PaginationResult $posts */
$posts = $this->find('lampager', [
    // Lampager options
    'forward' => true,
    'seekable' => true,
    'cursor' => [
        'Post' => [
            'id' => '4',
            'created' => '2017-01-01 10:00:00',
        ],
    ],

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

foreach ($posts as $post) {
    /** @var mixed[][] $post */
    debug($post['Post']['id']);
    debug($post['Post']['created']);
    debug($post['Post']['modified']);
}

static LampagerPaginator::create(Model $builder, array $options): static
LampagerPaginator::__construct(Model $builder, array $options)

LampagerPaginator::transform(\Lampager\Query $query): array

LampagerPaginator::build(array $cursor = []): array

LampagerPaginator::paginate(array $cursor = []): \Lampager\PaginationResult

object(Lampager\PaginationResult)#1 (5) {
  ["records"]=>
  array(3) {
    [0]=>
    array(1) {
      ["Post"]=>
      array(3) { ... }
    }
    [1]=>
    array(1) {
      ["Post"]=>
      array(3) { ... }
    }
    [2]=>
    array(1) {
      ["Post"]=>
      array(3) { ... }
    }
  }
  ["hasPrevious"]=>
  bool(false)
  ["previousCursor"]=>
  NULL
  ["hasNext"]=>
  bool(true)
  ["nextCursor"]=>
  array(1) {
    ["Post"]=>
    array(2) {
      ["id"]=>
      string(1) "3"
      ["created"]=>
      string(19) "2017-01-01 10:00:00"
    }
  }
}

LampagerTransformer::__construct(Model $builder, array $options)

class PostsController extends AppController
{
    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' => 10,
        ];

        /** @var \Lampager\PaginationResult $posts */
        $posts = $this->Paginator->paginate(Post::class);
        $this->set('posts', $posts);
    }
}

// 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,
    ]);
}