Home > OS >  Why I can't access to the paginator DTO in EasyAdmin?
Why I can't access to the paginator DTO in EasyAdmin?

Time:10-19

I'm trying to access the EasyCorp\Bundle\EasyAdminBundle\Dto\PaginatorDtoin my Crud Controller :

public function __construct(
        private EntityManagerInterface $manager,
        private EntityRepository $entityRepository,
        private PaginatorDto $paginatorDto,
    ) {
    }

But I've got this error => Cannot autowire service "App\Controller\Activity\ActivityCrudController": argument "$paginatorDto" of method "__construct()" references class "EasyCorp\Bundle\EasyAdminBundle\Dto\PaginatorDto" but no such service exists. and I don't understand why and How to fix it :(

Any idea ?

CodePudding user response:

I'm not an expert of that bundle so take my answer with a pinch of salt but looking at bundle's code I've noticed PaginatorDto not to be a service (as the name suggests).

As that DTO is not a service (and it's ok it is not), you can't autowire it nor make it a service "locally" (eg.: in your application).

So, in order to retrieve the DTO object, inject AdminContextProvider (that is a service as you can notice here) instead and use it to get the DTO

$adminContext->getCrud()->getPaginator();

CodePudding user response:

Your crud controller should extend AbstractCrudController which give you access to the current admin context.

So if you want to use it in one of your crud controller method you should be able to access the paginator with:

$paginator = $this->getContext()->getCrud()->getPaginator();

If you want to do the same outside your crud controller, let's say in another service. You need to inject the AdminContextProvider to first get the AdminContext and do it the same way.

private ?AdminContext $siteRepository;

public function __construct(AdminContextProvider $adminContextProvider)
{
    $this->adminContext = $adminContextProvider->getContext();
}
  • Related