0
votes

I have some simple pagination set up in my controller like so:

public function index() {
    $this->set('items', $this->paginate());
}

In my view I'm using the Paginator helper to output numbered links:

echo $this->Paginator->numbers(array(
    'separator' => '',
    'tag' => 'li',
    'currentTag' => 'a',
    'currentClass' => 'active'
));

This all works fine, however I want to use a custom URL for the paginated links. I added this to my routes.php file:

Router::connect('/things/:page', array('controller' => 'things', 'action' => 'index'), array('page' => '[0-9]+'));

Now the links outputted by the Paginator helper are the way I want. They look like http://mysite.com/things/2, http://mysite.com/things/3 etc.

But when I click the links the Paginator in my controller doesn't seem to recognize it's on a certain page, as I just get the first page's results shown. I'm guessing I need to somehow pass the page number to $this->paginate(), but I don't know what the best method is. Is there a way for Cake to get the page number automatically if I modify my route?

Thanks!

1

1 Answers

0
votes

Since the Pagination component by default expect named parameter 'page:1' you need somehow to pass same variable in index.

if you make print of $this->request->params in your controller, you will see that it's missing.

See that example how you can pass named parameters in the Router:

Router::connect(
   '/:controller/:action/*',
   array(),
   array(
    'named' => array(
        'wibble',
        'fish' => array('action' => 'index'),
        'fizz' => array('controller' => array('comments', 'other')),
        'buzz' => 'val-[\d]+'
        )
    )
);

For more info see this section in Cakephp book