KnpLabs/KnpPaginatorBundle is tracked by TopGit as a backend project, with 1.8k stars on GitHub, written primarily in PHP. SEO friendly Symfony paginator to sort and paginate
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Generally this bundle is based on Knp Pager component. This
component introduces a different way of 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 v3.0 or v4.X tags in the repository which is
suitable to paginate ODM MongoDB and ORM 2.0 queries
Latest updates
For details regarding changes please read about the releases.
Requirements:
Knp Pager component >=4.4.
KnpPaginatorBundle's master is compatible with Symfony >=6.4 versions.
Twig >=3.0 version is required if you use the Twig templating engine.
If you want to use the provided templates, you need the Translation component too.
Features:
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 concerns, 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.
More detailed documentation:
Creating custom pagination subscribers
Customizing view templates and arguments
Installation and configuration:
Pretty simple with Composer, run
composer require knplabs/knp-paginator-bundle
Add PaginatorBundle to your application kernel
If you don't use flex (you should), you need to manually enable bundle:
// app/AppKernel.php
public function registerBundles()
{
return [
// ...
new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
// ...
];
}
Configuration example
You can configure default query parameter names and templates, and a few other options:
YAML:
knp_paginator:
convert_exception: false # throw a 404 exception when an invalid page is requested
page_range: 5 # number of links shown in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6)
remove_first_page_param: false # remove the page query parameter from the first page link
default_options:
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
filter_field_name: filterField # filter field query parameter name
filter_value_name: filterValue # filter value query parameter name
page_out_of_range: ignore # ignore, fix, or throwException when the page is out of range
default_limit: 10 # default number of items per page
template:
pagination: '@KnpPaginator/Pagination/sliding.html.twig' # sliding pagination controls template
rel_links: '@KnpPaginator/Pagination/rel_links.html.twig' # <link rel=...> tags template
sortable: '@KnpPaginator/Pagination/sortable_link.html.twig' # sort link template
filtration: '@KnpPaginator/Pagination/filtration.html.twig' # filters template
PHP:
// config/packages/paginator.php
<?php declare(strict_types=1);
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $configurator): void
{
$configurator->extension('knp_paginator', [
'convert_exception' => false, // throw a 404 exception when an invalid page is requested
'page_range' => 5, // number of links shown in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links
'remove_first_page_param' => false, // remove the page query parameter from the first page link
'default_options' => [
'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
'filter_field_name' => 'filterField', // filter field query parameter name
'filter_value_name' => 'filterValue', // filter value query parameter name
'page_out_of_range' => 'ignore', // ignore, fix, or throwException when the page is out of range
'default_limit' => 10 // default number of items per page
],
'template' => [
'pagination' => '@KnpPaginator/Pagination/sliding.html.twig', // sliding pagination controls template
'rel_links' => '@KnpPaginator/Pagination/rel_links.html.twig', // <link rel=...> tags template
'sortable' => '@KnpPaginator/Pagination/sortable_link.html.twig', // sort link template
'filtration' => '@KnpPaginator/Pagination/filtration.html.twig' // filters template
]
]);
};
Additional pagination templates
That could be used out of the box in knp_paginator.template.pagination key:
Doctrine\Common\Collection\ArrayCollection - any Doctrine relation collection including
ModelCriteria - Propel ORM query
array with Solarium_Client and Solarium_Query_Select as elements
// App\Controller\ArticleController.php
public function listAction(EntityManagerInterface $em, PaginatorInterface $paginator, Request $request)
{
$dql = "SELECT a FROM AcmeMainBundle:Article a";
$query = $em->createQuery($dql);
$pagination = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1), /* page number */
10 /* limit per page */
);
// parameters to template
return $this->render('article/list.html.twig', ['pagination' => $pagination]);
}
View
In <head>:
{# rel links for pagination #}
{{ knp_pagination_rel_links(pagination) }}
In <body>:
{# total items count #}
<div class="count">
{{ pagination.getTotalItemCount }}
</div>
<table>
<tr>
{# sorting of properties based on query components #}
<th>{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}</th>
<th{% if pagination.isSorted('a.title') %} class="sorted"{% endif %}>
{{ knp_pagination_sortable(pagination, 'Title', 'a.title') }}
</th>
<th{% if pagination.isSorted(['a.date', 'a.time']) %} class="sorted"{% endif %}>
{{ knp_pagination_sortable(pagination, 'Release', ['a.date', 'a.time']) }}
</th>
</tr>
{# table body #}
{% for article in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ article.id }}</td>
<td>{{ article.title }}</td>
<td>{{ article.date | date('Y-m-d') }}, {{ article.time | date('H:i:s') }}</td>
</tr>
{% endfor %}
</table>
{# display navigation #}
<div class="navigation">
{{ knp_pagination_render(pagination) }}
</div>
Translation in view
For translating the following text:
%foo% name with translation key table_header_name. The translation is in the domain messages.
{0} No author|{1} Author|[2,Inf] Authors with translation key table_header_author. The translation is in the domain messages.
translationCount and translationParameters can be combined.
<table>
<tr>
{# sorting of properties based on query components #}
<th>{{ knp_pagination_sortable(pagination, 'Id'|trans({foo:'bar'},'messages'), 'a.id' )|raw }}</th>
<th{% if pagination.isSorted('a.Title') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Title', 'a.title')|raw }}</th>
<th>{{ knp_pagination_sortable(pagination, 'Author'|trans({}, 'messages'), 'a.author' )|raw }}</th>
</tr>
<!-- Content of the table -->
</table>
Adding translation files
You can also override translations by creating a translation file in the following name format: domain.locale.format.
So, to create a translation file for this bundle you need to create for instance KnpPaginatorBundle.tr.yaml file under project_root/translations/
and add your translations there:
If you set default translation for configuration accordingly:
framework:
default_locale: tr
Symfony will pick it automatically.
Dependency Injection
You can automatically inject a paginator service into another service by using the knp_paginator.injectable DIC tag.
The tag takes one optional argument paginator, which is the ID of the paginator service that should be injected.
It defaults to knp_paginator.
The class that receives the KnpPaginator service must implement Knp\Bundle\PaginatorBundle\Definition\PaginatorAwareInterface.
If you're too lazy you can also just extend the Knp\Bundle\PaginatorBundle\Definition\PaginatorAware base class.
⚠ Warning using PaginatorAwareInterface is discouraged, and could be removed in a future version. You should not rely on setter
injection, but only on proper constructor injection. Using Symfony built-in autowiring mechanism is the suggested way to go.
Lazy service
The knp_paginator service will be created lazily if the package symfony/proxy-manager-bridge is installed.
For more information about lazy services, consult the Symfony documentation on dependency injection.
If your locale is not available, create your own translation file in
translations/KnpPaginatorBundle.en.yml (substitute "en" for your own language code if needed).
Then add these lines:
label_next: Next
label_previous: Previous
Note that <rel> links are only meaningful when using pagination, they are not relevant to sorting or filtering.
Maintainers
Please read this post first.
This library is maintained by the following people (alphabetically sorted) :
Yes — KnpLabs/KnpPaginatorBundle ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/KnpLabs/KnpPaginatorBundle.
What else is in the Backend space?
KnpLabs/KnpPaginatorBundle is tracked by TopGit under the Backend category, alongside 6 GitHub-tagged topics. Trending and Topics pages list peer repositories of comparable stars and language.
What is KnpLabs/KnpPaginatorBundle?
KnpLabs/KnpPaginatorBundle (KnpLabs/KnpPaginatorBundle) is a PHP project on GitHub. From the project's own README: SEO friendly Symfony paginator to sort and paginate
What license does KnpLabs/KnpPaginatorBundle use?
KnpLabs/KnpPaginatorBundle is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where can I see KnpLabs/KnpPaginatorBundle in action?
The project maintains a homepage at http://knplabs.com/en/blog/knp-paginator-reborn. The README tab on this page also usually contains screenshots and a quickstart.
Where do I read more about KnpLabs/KnpPaginatorBundle?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/KnpLabs/KnpPaginatorBundle is the definitive source.
Why is KnpLabs/KnpPaginatorBundle categorized under Backend?
TopGit places KnpLabs/KnpPaginatorBundle in the Backend category based on its GitHub topics and description (tagged: "paginator", "php", "seo"). Categories are assigned from real repository metadata, not editorial guesswork.
Read full README in the tab above.
Still deciding about KnpPaginatorBundle?
One click hands the question to an AI along with this page — see what it says about KnpPaginatorBundle.