Symfony: Page Navigation Links

Pagination helper in the code snippets section of Symfony’s website.

The code is modified to include <ul> and <li> tags and to correctly parse uri when passed as routing rule:

function page_navigation($pager, $uri)
{
  $pages = array();
 
  if ($pager->haveToPaginate())
  {
    $uri .= (preg_match('/\?/', $uri) ? '&' : '?').'page=';
 
    // first / prev links
    $pages[] = link_to('&laquo; First', $uri . '1');
    $pages[] = link_to('&lt; Prev', $uri . $pager->getPreviousPage());
 
    // pages one by one
    $links = array();
    foreach ($pager->getLinks() as $page)
    {
      $pages[] = link_to_unless($page == $pager->getPage(), $page, $uri.$page);
    }
 
    // next / last links
    $pages[] = link_to('Next &gt;', $uri.$pager->getNextPage());
    $pages[] = link_to('Last &raquo;', $uri.$pager->getLastPage());
  }
 
  return '<ul><li>' . implode('</li><li>', $pages) . '</li></ul>';
}

Save the file as PaginationHelper.php in project/root/lib/helper or apps/application/lib/helper.

Edit template file:

<?php echo use_helper('Pagination') ?>
<?php echo pager_navigation($page, '@routing') ?>

Leave a Reply

Your email address will not be published. Required fields are marked *