1. Go to this page and download the library: Download lampager/lampager-cakephp 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-cakephp example snippets
namespace App\Controller;
use Cake\Controller\Controller;
use Lampager\Cake\Datasource\Paginator;
class AppController extends Controller
{
public $paginate = [
'className' => Paginator::class,
];
}
namespace App\Model\Table;
use Cake\ORM\Table;
use Lampager\Cake\Model\Behavior\LampagerBehavior;
class AppTable extends Table
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->addBehavior(LampagerBehavior::class);
}
}
namespace App\Controller;
class PostsController extends AppController
{
public $Posts = null;
/**
* This method shows how to pass options by a query and array.
*/
public function query(): void
{
// Get cursor parameters
$previous = json_decode($this->request->getQuery('previous_cursor'), true);
$next = json_decode($this->request->getQuery('next_cursor'), true);
$cursor = $previous ?: $next ?: [];
// Query expression can be passed to PaginatorComponent::paginate() as normal
$query = $this->Posts
->where(['Posts.type' => 'public'])
->orderByDesc('created')
->orderByDesc('id')
->limit(15);
/** @var \Lampager\Cake\PaginationResult<\Cake\ORM\Entity> $posts */
$posts = $this->paginate($query, [
// If the previous_cursor is not set, paginate forward; otherwise backward
'forward' => !$previous,
'cursor' => $cursor,
'seekable' => true,
]);
$this->set('posts', $posts);
}
/**
* This method shows how to pass options from an array.
*/
public function options(): void
{
// Get cursor parameters
$previous = json_decode($this->request->getQuery('previous_cursor'), true);
$next = json_decode($this->request->getQuery('next_cursor'), true);
$cursor = $previous ?: $next ?: [];
/** @var \Lampager\Cake\PaginationResult<\Cake\ORM\Entity> $posts */
$posts = $this->paginate('Posts', [
// Lampager options
// If the previous_cursor is not set, paginate forward; otherwise backward
'forward' => !$previous,
'cursor' => $cursor,
'seekable' => true,
// PaginatorComponent config
'conditions' => [
'type' => 'public',
],
'order' => [
'created' => 'DESC',
'id' => 'DESC',
],
'limit' => 15,
]);
$this->set('posts', $posts);
}
}
// If there is a next page, print pagination link
if ($posts->hasPrevious) {
echo $this->Html->link('<< Previous', [
'controller' => 'posts',
'action' => 'index',
'?' => [
'previous_cursor' => json_encode($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' => json_encode($posts->nextCursor),
],
]);
}