KnpLabs / KnpPaginatorBundle

SEO friendly Symfony paginator to sort and paginate
http://knplabs.com/en/blog/knp-paginator-reborn
MIT License
1.76k stars 341 forks source link

Problem with autowire PaginationInterface #713

Closed JuergenFB closed 3 years ago

JuergenFB commented 3 years ago
Q A
Bundle version 5 6.0
Symfony version 4.4.30
PHP version 7.4.3

Support Question

When open my site I get the following error message:

Cannot autowire argument $paginator of "App\Controller\messageController::listMessagesAction()": it references interface "Knp\Component\Pager\Pagination\PaginationInterface" but no such service exists. Did you create a class that implements this interface?

I upgrade my project from Symfony 3.4 to 4.4 using the SymfonyCasts "Upgrade to Symfony4 and Flex!" So I'm using Flex.

Controller code:

class messageController extends BaseController
{
    /**
     * @Route("/messages", name="messageList")
     */
    public function listMessagesAction(Request $request, PaginationInterface $paginator)
    {
        // ...
        //$paginator = $this->get('knp_paginator');  <- old Symfony 3.4 code
        $pagination = $paginator->paginate(
            $query,
            $page,
            $user->getTableRows(),
            [
                'defaultSortFieldName' => 'm.lastAnswerCreatedAt',
                'defaultSortDirection' => 'desc'
            ]
        );
       //...
   }

Configuration of knp_paginator:

Current configuration for extension with alias "knp_paginator"
==============================================================

knp_paginator:
    page_range: 10
    default_options:
        page_name: page
        sort_field_name: sort
        sort_direction_name: direction
        distinct: true
        filter_field_name: filterField
        filter_value_name: filterValue
        page_out_of_range: ignore
        default_limit: 10
    template:
        pagination: Helper\gridPagination.template.twig
        filtration: '@KnpPaginator/Pagination/filtration.html.twig'
        sortable: '@KnpPaginator/Pagination/sortable_link.html.twig'
    page_limit: null

Result of bin/console debug:autowiring:

Autowirable Types
=================
...
PaginatorInterface
 Knp\Component\Pager\PaginatorInterface (knp_paginator)
...

How to solve the issue?

Thanks in advance.

garak commented 3 years ago

It looks like you're confusing paginator with pagination.

JuergenFB commented 3 years ago

Thanks a lot, after changing type hint from PaginationInterface to PaginatorInterface the issue is solved.