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()".
As it says in the documentation. You must extend the base
Controller
class, or use dependency injection instead https://symfony.com/doc/current/service_container.html#service-parametersIn my case I use
AbstractController
and asmalcolm
says, it is beter to inject the service directrly in your action, even so, I call a method several times and I think that overwritegetSubscribedServices
is clener for my porpuse.You have to extend
Controller
instead ofAbstractController
class:or better leave
AbstractController
and injectknp_paginator
service into your action: