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

后端 未结 4 803
青春惊慌失措
青春惊慌失措 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条回答
  •  萌比男神i
    2021-01-12 00:29

    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()...
        }
    

提交回复
热议问题