Route + Pagination - Cake 2.x

Asked

Viewed 86 times

1

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

I tried that:

  • 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 of the site.com.br/noticias are correct (site.com.br/noticias/2 to page 2, site.com.br/noticias/3 to page 3, etc). But when opening pages 2, 3, 4, etc., it always shows the result of page 1.

I gave one debug($page) controller, but I don’t know if I put it in the right place.

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

How to solve this?

1 answer

0

I’m not sure, but try pulling this route

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

and you should be able to receive the page parameter correctly

Browser other questions tagged

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