Search code examples
zend-frameworkzend-routezend-paginator

zend pagination url format


i've followed the pagination tutorial from http://framework.zend.com/manual/en/zend.paginator.usage.html

I have successfully implemented pagination for my site, but i am not satisfied with the URLs output for the paging. example url for page 2:

http://www.example.com/posts/index/page/2

What i would like is to remove the index and just have http://www.example.com/posts/page/2

Why is index included while accessing this->url(in the my_pagination_control.phtml from tutorial in link)?

Is there a way to gracefully just show posts/page/2? or even just posts/2?


Solution

  • I feel that the previous answer is not enough, I'll give mine. First of all you can add a router in your bootstrap.php that looks like:

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {
       protected function _initRoutes()
       {
          $Router = Zend_Controller_Front::getInstance()->getRouter();
    
          $Route = new Zend_Controller_Router_Route(
                          ':controller/*',
                          array(
                              'controller' => 'index',
                              'action' => 'index'
                          )
          );
          $Router->addRoute('paginator1', $Route);
    
          $Route = new Zend_Controller_Router_Route(
                          ':controller/:page/*',
                          array(
                              'controller' => 'index',
                              'action' => 'index',
                          ),
                          array(
                              'page' => '[0-9]+'
                          )
          );
          $Router->addRoute('paginator2', $Route);
       }
    
    }
    

    and then, use in your view this simple line:

    echo $this->url(array('controller' => 'CONTROLLER-NAME', 'page' => 5), 'paginator1', TRUE);
    echo $this->url(array('controller' => 'CONTROLLER-NAME', 'page' => 5), 'paginator2', TRUE);
    

    In the case of 'paginator1', the url will be printed in this way:

    /CONTROLLER-NAME/page/5
    

    In the case of 'paginator2', the url will be printed in this way:

    /CONTROLLER-NAME/5
    

    Obviously where you see CONTROLLER-NAME will be the name of the controller you write.