'Set page number in controller in pagination CakePHP 4.x
Is it possible to set the page number programatically within the controller? I have a searchform on a paginated index page. Each time the user send a new search I want to reset the page number to 1.
In the request params there is 'page' with the current number, so when I paginate the query with the new search conditions the page number remains the same. I want it to be 1.
I tried set page in paginate array:
$this->paginate = array(
'limit'=> 20,
'order'=> array('Productions.id' => 'desc'),
'contain'=> array(
'Products'=>'Units',
'Telephely',
'Leccsapat'
),
'recursive'=> -1,
'conditions'=>$conditions,
'page'=> 1,
);
No success.
I tried
$this->paginate['offset']=0;
No success.
I tried also
public function initialize(): void
{
parent::initialize();
$this->loadComponent('Paginator');
}
.
.
.
$this->Paginator->setConfig('page',1);
No success. I can save the search condition in session, and then redirect in controller with page:1, but I prefer to do it in pagination.
Solution 1:[1]
In controller try overwrite page number. Not very pretty, but effective:
if ($this->getRequest()->is('post')) {
$queryParams = $this->getRequest()->getQueryParams();
$queryParams['page'] = 1;
$this->setRequest($this->getRequest()->withQueryParams($queryParams));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |