Tag Archives: pass parameters

Using pass parameters with the Paginator component

CakePHP’s paginator is a great component allowing you to paginate what you want.

Though, I went through a “problem”. I wanted to make custom routes and I didn’t find how to set the paginator to use the pass parameters (example : /posts/index/1) instead of the named params (example : /posts/index/page:1) or the query string (example : /posts/index/?page=1). The problem was occuring to get the links using the methods next and prev of the paginator in the view, always giving me named parameters or query string.

My aim was to write a route like this /news-page-1 where 1 is the page number, and a pass parameter !

I did a small hack providing a custom route with pass parameters. This problem is maybe due to my lack of knowledge concerning this component, but I couldn’t find anything to use pass parameters for it.

We will see how to do it through an example with Post.

PostsController

 
   //using the Paginator component
   public $components = array("Paginator");

   //index method
   public function index($page = 1)
   {
        $this->Paginator->settings = array
        (
            'Post' => array
            (
                'limit' => 2,
                'page' => $page,
                'conditions' => array
                (
                    'published' => 1
                ),
                'order' => array
                (
                    'created' => 'DESC'
                ),
            )
        );

        $posts = $this->paginate();           
	$this->set('posts', $posts);
    }

It’s pretty much easy to see what we are doing here. We limit the number of Posts on each page to 2, we want only the published posts and we want them ordered by created desc. I added the $page variable which is there to say which page we are looking for.

Let’s deal with the custom route we wanted, remember? We want /news-page-1 (feel free to do the route you want :p)

app/Config/routes.php

    //setting the route with the pass parameter page
    Router::connect
    (
        '/news-page-:page', 
        array('controller' => 'posts'), 
        array
        (
            'pass' => array('page'), 
            'page' => '[0-9]+'
        )
    );

Let’s see how to generate the link for the view.
View/Posts/index.ctp

    //put this where you want the previous link to be            
    if($this->Paginator->hasPrev()):

        //grab the current page number           
        $page = $this->params['paging']['Post']['page'];
        
        //decrement to get the previous page
        $page--;

        //generating the link
        //giving $page as a pass parameter
        echo $this->Html->link
        (
            '← Previous', 
            array
            (
                'controller' => 'posts', 
                'action' => 'index', 
                'page' => $page
            ),
            array('escape' => false)
        ); 

    endif;

You can notice we don’t use the prev method of the paginator to generate the link. You can also write the next link, the same way than for the previous, but use the hasNext method to check if there is a next page and increment $page. Also change the name of the link 😉

Et voilà we have a nice url using pass parameters with the paginator component.

P.S : feel free to give feedback about the paginator, if you found a way to get it working with pass parameters, thanks.