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

后端 未结 4 813
青春惊慌失措
青春惊慌失措 2021-01-12 00:01

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 i

4条回答
  •  一个人的身影
    2021-01-12 00:17

    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);
        //...
    
    }
    

提交回复
热议问题