Symfony 4 - KnpPaginator Bundle "service not found, even though it exists in app's container"

symfony 4 pagination example
symfony 4 doctrine pagination
symfony 4 doctrine pagination example
knp-paginator-bundle symfony 3
symfony 3 pagination example
there are no registered paths for namespace "knppaginator".
knplabs/knp-paginator-bundle packagist

I have been following tutorials, and all instructions show it's done the exact same way, but it doesn't seem to work in Symfony 4. Is there something I'm overlooking or is the bundle simply incompatible?

I ran: composer require knplabs/knp-paginator-bundle

It was loaded automatically into bundles.php, thanks to Flex.

Inserted the following into config/services.yaml:

    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:twitter_bootstrap_v3_pagination.html.twig     # sliding pagination controls template
        sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig                         # sort link template

Tried to use the following in the controller:

$paginator  = $this->get('knp_paginator');

and got the following error:

Service "knp_paginator" not found: even though it exists in the app's container, the container inside "App\Controller\PhotoController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "request_stack", "router", "security.authorization_checker", "security.token_storage", "serializer", "session" and "twig" services. Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "PhotoController::getSubscribedServices()".

You have to extend Controller instead of AbstractController class:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller

    public function myAction()
        $paginator  = $this->get('knp_paginator');

or better leave AbstractController and inject knp_paginator service into your action:

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Knp\Component\Pager\PaginatorInterface;

class MyController extends AbstractController

    public function myAction(PaginatorInterface $paginator)

Compatibility Symfony 4.0.x ? · Issue #468 · KnpLabs , In Symfony 3, the KnpPaginatorBundle is the perfect solution when you work with Doctrine Duration: 4:50 Posted: Mar 29, 2019 I’m looking to implement KnpPaginatorBundle into symfony4. My issue here is that , in their documentation (meant for S3) they recommend using the following parameters i config.yml. knp_paginator:

In my case I use AbstractController and as malcolm says, it is beter to inject the service directrly in your action, even so, I call a method several times and I think that overwrite getSubscribedServices is clener for my porpuse.

public static function getSubscribedServices(): array
    $services = parent::getSubscribedServices();
    $services['fos_elastica.manager'] = RepositoryManagerInterface::class;
    $services['knp_paginator'] = PaginatorInterface::class;

    return $services;

private function listHandler(Search $search, Request $request, int $page): Response
    $repository = $this->container->get('fos_elastica.manager')->getRepository(Foo::class);


How to install the KnpPaginatorBundle to paginate Doctrine queries , Just add class namespace of KnpPaginatorBundle : in config/bundles.php : <?​php return  Fix phpunit 4 requiring symfony/yaml <4 By ostrolucky , 2 years ago add symfony 4 to travix build matrix

As it says in the documentation. You must extend the base Controller class, or use dependency injection instead

pagination in symfony 4 using KnpPaginatorBundle, KnpPaginatorBundle. by KnpLabs. SEO friendly Symfony paginator to sort and paginate. Infos. License: MIT; Score: 808; Last update: 2018-03-  Browse other questions tagged symfony symfony-forms symfony-2.1 symfony4 or ask your own question. The Overflow Blog Podcast 228: chatting with Stack Overflow’s community developers

In my case i'm using symfony 4.3, I just injected the class to the methods as an argument and i'm done.

public function list(ProductManager $productManager)
   $products = $productManager->prepareProducts(); 
   return $products;

KnpPaginatorBundle by KnpLabs, composer require knplabs/knp-paginator-bundle. While that's installing Depending on your setup, that may or may not even be possible in Symfony 4. And, in  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.

Pagination > Mastering Doctrine Relations!, It is, but it's also compatible with Symfony 3. Installing the bundle is largely copy / paste from Posted: Aug 8, 2016 Hi, I'm having a problem using this bundle with Symfony 4.0. I install it via : composer require knplabs/knp-paginator-bundle I then add the basic configuration in my config file Finally I followed the example of the method "listAction (

Pagination with Twig and KnpPaginatorBundle, Symfony 3 - Pagination with Twig and KnpPaginatorBundle (Part 2/14) - Duration​: 8:00. Code Duration: 15:46 Posted: Aug 7, 2018 Friendly Symfony 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.

KNP Pagination in Symfony 4, The KnpPaginatorBundle is a great bundle for pagination inside Symfony. The bundle requires Symfony >=3.4 and Twig >=2.0 if you use the  Creates empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code. This bundle is an alternative to SensioGeneratorBundle for modern Symfony applications and requires using Symfony 3.4 or newer and Symfony Flex. API Platform A set of tools to build and consume web APIs.