Find file
Fetching contributors…
Cannot retrieve contributors at this time
178 lines (135 sloc) 5.84 KB

Intro to KnpPaginatorBundle

SEO friendly Symfony2 paginator to paginate everything

Generally this bundle is based on Knp Pager component. This component introduces a different way for pagination handling. You can read more about the internal logic on the given documentation link.

Note: Keep knp-components in sync with this bundle. If you want to use older version of KnpPaginatorBundle - use v1.0 tag in the repository which is suitable to paginate ODM mongodb and ORM 2.0 queries

Latest updates

For notes about latest changes please read CHANGELOG, for required changes in your code please read UPGRADE chapter of documentation.


  • Knp pager component >=1.1
  • KnpPaginatorBundle's master compatible with symfony (>=2.0 versions).
  • Twig >=1.5 version is required if you use twig templating engine


  • Does not require initializing specific adapters
  • Can be customized in any way needed, etc.: pagination view, event subscribers.
  • Possibility to add custom filtering, sorting functionality depending on request parameters.
  • Separation of conserns, paginator is responsible for generating the pagination view only, pagination view - for representation purposes.

Note: using multiple paginators requires setting the alias in order to keep non conflicting parameters. Also it gets quite complicated with a twig template, since hash arrays cannot use variables as keys.

More detailed documentation:

Installation and configuration:

Pretty simple with composer, add:

    require: {
        "knplabs/knp-paginator-bundle": "dev-master"

If you use a deps file, add:



Or if you want to clone the repos:

# Install Knp components
git clone git:// vendor/knp-components

# Install knp paginator bundle
git clone git:// vendor/bundles/Knp/Bundle/PaginatorBundle

Configuration example

You can configure default query parameter names and templates

    page_range: 5                      # default page range used in pagination control
        page_name: page                # page query parameter name
        sort_field_name: sort          # sort field query parameter name
        sort_direction_name: direction # sort direction query parameter name
        distinct: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
        pagination: KnpPaginatorBundle:Pagination:sliding.html.twig     # sliding pagination controls template
        sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template

Add the namespaces to your autoloader unless you are using composer

// File: app/autoload.php
    'Knp\\Component'      => __DIR__.'/../vendor/knp-components/src',
    'Knp\\Bundle'         => __DIR__.'/../vendor/bundles',
    // ...

Add PaginatorBundle to your application kernel

    // File: app/AppKernel.php
    public function registerBundles()
        return array(
            // ...
            new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
            // ...

Usage examples:


Currently paginator can paginate:

  • array
  • Doctrine\ORM\Query
  • Doctrine\ORM\QueryBuilder
  • Doctrine\ODM\MongoDB\Query\Query
  • Doctrine\ODM\MongoDB\Query\Builder
  • Doctrine\Common\Collection\ArrayCollection - any doctrine relation collection including
  • ModelCriteria - Propel ORM query
  • array with Solarium_Client and Solarium_Query_Select as elements
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT a FROM VendorBlogBundle:Article a";
$query = $em->createQuery($dql);

$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
    $this->get('request')->query->get('page', 1)/*page number*/,
    10/*limit per page*/

// parameters to template
return compact('pagination');


{# sorting of properties based on query components #}
    <th>{{ knp_pagination_sortable(pagination, 'Id', '') }}</th>
    <th{% if pagination.isSorted('a.Title') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Title', 'a.title') }}</th>

{# table body #}
{% for article in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
    <td>{{ }}</td>
    <td>{{ article.title }}</td>
{% endfor %}
{# display navigation #}
<div class="navigation">
    {{ knp_pagination_render(pagination) }}