I am upgrading to CakePHP 4.4, and the Paginator component I was using previously has been deprecated.
I am using the pagination on a controller that’s meant for an API endpoint, which means, I have no templates because I’m using a JSON view:
public function initialize(): void
{
parent::initialize();
$this->viewBuilder()
->setClassName('Json');
}
In CakePHP 4.3, I used to be able to call $this->Paginator->getPagingParams()
to access the page count, which I was then outputting together with my results, IE:
public function list(int $limit = 10, int $page = 1)
{
$query = $this->People->find('visible')
->order(['lastname' => 'ASC']);
$paginateSettings = [
'limit' => $limit,
'page' => $page,
'maxLimit' => 100
];
$this->set('people', $this->paginate($query, $paginateSettings));
$this->set('page', $page);
$pagingParams = $this->Paginator->getPagingParams();
$this->set('pages', $pagingParams['People']['pageCount']);
$this->viewBuilder()
->setOption('serialize', ['people', 'page', 'pages']);
}
This was working perfectly, allowing me to use the page
and pages
on the client side to navigate the pagination.
Where can I find the total number of pages now? Am I supposed to use the PaginatorHelper
? I see from the documentation that it has a total
method.
However, how can I access that in a controller? I’ve tried adding ->addHelper('Paginator')
both to my initialize
and beforeRender
methods in the controller but I still can’t access $this->Paginator
.
Is this the right way, and I’m somehow doing it wrong? Or is there another way to get the total number of pages that the paginator is creating without using PaginatorHelper
?