How to add the get parameters in the pagination on CakePHP version 2.0

Problem Statement

There is no default functionality to persist the GET parameters while using “Paginator” helper in CakePHP. As per default functionality of CakePHP paginatior helper, the GET parameters will remove from the URL if you navigate to the next page. So in this blog we will learn how to add the GET parameters in the URL of page number in CakePhp version 2.0?

Solution

If you are facing this issue then you need to add a few lines of code in the beforeRender function. This function is defined in the paginator.php file (File Location: cake\libs\view\helpers\paginator.php). You need to add the following lines shown in the brown color:

public function beforeRender($viewFile) {
            $this->options['url'] = array_merge($this->request->params['pass'], $this->request->params['named']);
//Start of code
              if (!empty($this->request->query)) {
                       $get_param = $this->request->query;
                       unset($get_param['url']);
                       $this->options['url']['?'] = $get_param;
                }    
// End of code
                parent::beforeRender($viewFile);
}
Shivam Verma

Shivam Verma

Shivam Verma is an experienced software engineer in PHP development and Database design. His area of interest is website development. He likes to be aware of his surroundings and to learn new things by observing others. He believes that by doing this we can learn new things and can also enhance our knowledge everyday. He has started writing technical blogs with a view to help others in studying and learning new things in an easy way.

Leave a Reply

Your email address will not be published. Required fields are marked *