Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

knp_paginator:
    page_range:                 5          # default page range used in pagination control
    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
    template:
        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()".

like image 986
Darius Avatar asked Feb 12 '18 05:02

Darius


1 Answers

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)
    {
        $paginator->paginate()...
    }
like image 68
malcolm Avatar answered Sep 21 '22 19:09

malcolm