Symfony 4 - KnpPaginator Bundle "service not found, even though it exists in app's container"
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()...
}
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);
//...
}