How to install the KnpPaginatorBundle to paginate Doctrine queries in Symfony 4

Performance is an important feature of every web application. One of the most necessary things in applications that handle large sets of Data, is the implementation of a paginator. So the user can navigate through the collection of data without loading thousand of items at once. In Symfony 3, the KnpPaginatorBundle is the perfect solution when you work with Doctrine, however with the introduction of Symfony 4, the installation and configuration of this utility isn't well documented and most of the developers will end up implementing another package for sake of simplicity. But don't worry, the installation of this bundle is a piece of cake if you follow our steps. KnpPaginatorBundle can paginate:

array

Doctrine\ORM\Query

Doctrine\ORM\QueryBuilder

Doctrine\ODM\MongoDB\Query\Query

Doctrine\ODM\MongoDB\Query\Builder

Doctrine\ODM\PHPCR\Query\Query

Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder

Doctrine\Common\Collection\ArrayCollection- any doctrine relation collection including

ModelCriteria- Propel ORM query

array withSolarium_ClientandSolarium_Query_Selectas elements

In this article, we'll show you how to install the widely know KnpPaginatorBundle in your Symfony 4 based project.

1. Install and configure KnpPaginatorBundle

The KnpPaginatorBundle is a package for Symfony that allows you to use a SEO friendly Symfony paginator to paginate everything. This utility:

Does not require initializing specific adapters

Can be customized in any way needed, etc.: pagination view, event subscribers.

After installing the package, you only need to create the configuration file of the paginator in the app/config/packages directory. This file is the knp_paginator.yaml that contains the configuration of this utility. The configuration is up to you, but you can use a default configuration providen by the documentation:

2. Configure Paginator in Controller

Once the plugin is installed and configured, it's usage is pretty simple. Just be sure that your controllers extend the Symfony\Bundle\FrameworkBundle\Controller\Controller class as this is the one that allows you to access multiple services with the get method of the class. Just instantiate the paginator and keep it on a variable, then create some Doctrine query (important Query, not results) and then call the paginate method from the paginator and provide its 3 required arguments:

The Doctrine Query

The current page, defined by the get parameter "page" and set the first page as default when this parameter doesn't exist.

The limit of items per page.

You need to iterate over the returned element of the paginate method. This object contains the results of your query with the limit of results and other properties that you can access later in the view:

This will generate a list of 5 items on every page as indicated in our controller. The knp_pagination_render function renders a slider with all the available pages of the items. When clicking on every generated link, the same URL with all its query parameters will be used as well and the new parameter namely page that indicated our controller which items to return to the view.