What is correct way to use Pagerfanta for pagination in Symfony2

I have read that Pagerfanta is the pagination plugin of choice for Symfony2, but I am having some trouble getting it to work correctly. I have downloaded the code for pagerfanta and PagerfantaBundle and installed them as described in the WhiteOctober Github readme file and in this tutorial. I'm pretty sure the code in my controller is okay, but there is something wrong with my template file.

My controller code:

public function indexAction($page =  null)
    $query = $em->createQuery('SELECT something FROM some_table');
    $adapter = new DoctrineORMAdapter($query);
    $pager =  new Pagerfanta($adapter);
    if (!$page)    $page = 1;
    try  {
    catch(NotValidCurrentPageException $e) {
      throw new NotFoundHttpException('Illegal page');
    return $this->render('MyBundle:MyEntity:index.html.twig', array(
        'pager' => $pager,

In my template:

  {% for object in pager.currentPageResults %}
      <td>{{ object.attribute1 }}</td>
      <td>{{ object.attribute2 }}</td>
      <td>{{ object.attribute3 }}</td>
  {% endfor %}

{% if pager.haveToPaginate %}
  {{ pagerfanta(pager) }}
{% endif %}

When I go to the page, I get a list of the first 10 objects. However,the rendering of the pager is just PreviousNext as one word (not links and no pages 1, 2, 3, etc).

If I add ?page=2 to the URL, it doesn't display the next 10 items in the list.

I didn't change the route from what is was before I added the pagination code because the documentation says:

The routes are generated automatically for the current route using the variable "page" to propagate the page number.

... but I wonder if incorrect routing is part of the problem. Assuming this is the case, I'm not sure how to proceed with this.

I have been unable to find any instructions other the sources I have linked to above, so it would be great if someone who has implemented this successfully could share the details of how they did it.


I think your application doesn't know how to parse ?page=2 to the controller action.

Include a {page} in your route (as @AdrienBrault replied), which you wish to have pagination.

like this:

  pattern:  /{page}
  defaults: { _controller: MyTestBundle:Object:index, page: 1 }
    _method:  GET
    page: \d+

PagerFantaBundle will (I guess) try to guess the correct route.

Worked for me.

Need Your Help

How to use a pipe with select after forking and executing?

c++ linux select pipe

I'm a noob to linux programming, so please bear with me. In my application, I fork(), then execl() another binary after having setup a single pipe for reading in. After the fork and exec are OK, i ...

Mule not rolling back JMS message after exception

jms mule

My flow consumes a message based on cron expression and I have deliberately added a groovy code to throw exception for testing JMS rollback. Rollback doesn't return the consumed message back in que...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.