How to make a Slug pagination in Cakephp?

Asked

Viewed 281 times

4

Cakephp by default generates a pagination with indexes, for example:

noticias/listar/page:2

I would like to make a pagination in this way:

notícias/listar/2

1 answer

4


app/config/Routes.php

Router::connect('/noticias/:slug', array('controller' => 'noticias',
         'action'=>'listar', 'slug'=> '[0-9a-zA-Z]+'));
Router::connect('/noticias/:slug/:pagina', array('controller' => 'noticias',
         'action'=>'listar','slug'=> '[0-9a-zA-Z]+','pagina'=>'[0-9]+'));

Noticiascontroller:

$this->paginate = array(
//outras coisas aqui
'paramType' => 'querystring'
 );

View list:

$slug = $this->params['slug'];
$this->Paginator->options(array('url'=> array('controller' => 'noticias',
                              'action'=>'listar','slug'=> $slug),
                                 'convertKeys' => array('page')));

I think that’s it, you need to set the cake routes so he understands what you want to request.

  • This way when clicking on the page of the site will show news/list/1, where 1 is the first news registered in the database and if you click on next will show the other news in this way?

  • @Thiagophilipp , you keep the same pattern, what cake will do is take the non-Slug url and display the Slug pattern. Now, whether it will show the first, second, or 10 first, will depend on the configuration you used on $paginate, ex:. public $paginate = array('limit' => 1);

  • Dear @Thiago, did this solution suit you? ...

  • I managed to resolve, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.