Route + Pagination - Cake 2.x

1

I have a "classic" question about paging. I did as below, everything worked normally, but I could not mount the route for the pagination to stay as I want.

I tried this:

  • Controller

    class NoticiasController extends AppController {
    
                 /**
                * Lista as notícias utilizando paginação
                */
    
                public function lista($page = 1) {
    
                                     $options = array(
    
                                                         'fields' => array('Noticia.titulo', 'Noticia.resumo'),
    
                                                         'conditions' => array('Noticia.active' => true),
    
    
                                                         'page' => $page,
    
                                                         'order' => array('Noticia.created' => 'DESC'),
    
                                                         'limit' => 10
    
                                     );
    
                                     $this->paginate = $options;
    
                                     // Roda a consulta, já trazendo os resultados paginados
    
                                     $noticias = $this->paginate('Noticia');
    
                                     // Envia os dados pra view
    
                                     $this->set('noticias', $noticias);
    
                                     debug($page);
    
                }
    
    
    }
    
  • View

     <article>
    
     <?php foreach($noticias AS $data): ?>
    
                <h1><?php echo $data['Noticia']['titulo'] ?></h1>
    
                <p><?php echo $data['Noticia']['resumo'] ?></p>
    
     <?php endforeach; ?>
    
     </article>
    
    <?php
    
    echo $this->Paginator->prev('« Mais novas', null, null, array('class' => 'desabilitado'));
    
    echo $this->Paginator->numbers();
    
    echo $this->Paginator->next('Mais antigas »', null, null, array('class' => 'desabilitado'));
    
    ?>
    
  • Routes

    Router::connect('/noticias', array('controller' => 'noticias', 'action' => 'listar')); // pagina 1
    Router::connect('/noticias/:page', array('controller' => 'noticias', 'action' => 'listar'), array('pass' => array('page'))); // pagina 2 em diante
    

In this case, the links on the site.com.br/noticias page are correct (site.com.br/noticias/2 for page 2, site.com.br/noticias/3 for page 3, etc.). But when opening pages 2, 3, 4, etc., always shows the result of page 1.

I gave a debug($page) to the controller, but I do not know if I put it in the correct place.

It always gives (int) 1, that is, it seems that the $page value is not taken to the pages.

How to solve this?

    
asked by anonymous 06.02.2015 / 20:49

1 answer

0

I'm not sure, but try removing this route

Router::connect('/noticias/:page', array('controller' => 'noticias', 'action' => 'listar'), array('pass' => array('page')));

and you should already be able to receive the page parameter correctly

    
02.03.2015 / 18:28