Retain custom parameter or query string in pagination
Pagination in CakePHP 1.3 doesn't provide a way to retain custom query string passed with the url which is an essential part for any application.
Say if url is something like www.example.com/post/search?section=acticle&keyword=cakephp, the pagination links doesn't contain the query string passed.
But it can be easily achieved if we tweak pagination a bit. We can simply pass our query string to options['url'] of Paginator with key '?'.
I used options() function to set it so that it can be used for all links generated by Paginator.
//In view file before any call to Paginator Helper
//$this->params['url'] contains all the query string with key and value
$url_param = array_filter($this->params['url']); //strip out any parameter which doen't have any value
unset($url_param['url']); //its not the query string so unset it, its the path to our action with any parameter to it
$query_string = '';
//cteate the query string with key and value
foreach($url_param as $key => $val){
$query_string .= '&'.$key.'='.$val;
}
//set 'url' key which will be used by paginator helper to set our query string in pagination
$options['url'] = array_merge($this->passedArgs, array('?'=> $query_string));
//call the options function which will set options variable of Paginator Helpler
$this->Paginator->options($options);
//that's it. Now the Paginator will create links with our query string .
//$this->params['url'] contains all the query string with key and value
$url_param = array_filter($this->params['url']); //strip out any parameter which doen't have any value
unset($url_param['url']); //its not the query string so unset it, its the path to our action with any parameter to it
$query_string = '';
//cteate the query string with key and value
foreach($url_param as $key => $val){
$query_string .= '&'.$key.'='.$val;
}
//set 'url' key which will be used by paginator helper to set our query string in pagination
$options['url'] = array_merge($this->passedArgs, array('?'=> $query_string));
//call the options function which will set options variable of Paginator Helpler
$this->Paginator->options($options);
//that's it. Now the Paginator will create links with our query string .
