I followed the Upgrade Guide to upgrade Api Platform from version 3.4 to 4.0.
Before Api Platform 4.0, I was able to inject the Api Platform Paginator directly into my custom controller like this:
Entity Class:
<?php
namespace AppEntity;
use ApiPlatformMetadataApiResource;
use ApiPlatformMetadataGetCollection;
use DoctrineORMMapping as ORM;
#[ApiResource(
operations: [
new GetCollection(
controller: MyCustomController::class,
),
],
)]
#[ORMEntity(repositoryClass: MyEntityRepository::class)]
class MyEntity
{
}
Controller:
<?php
namespace AppController;
use ApiPlatformDoctrineOrmPaginator;
use SymfonyBundleFrameworkBundleControllerAbstractController;
class MyCustomController extends AbstractController
{
public function __invoke(
Paginator $data
)
{
// Some logic
return $data;
}
}
However, after upgrading to Api Platform 4, I encounter the following error:
{
"title": "An error occurred",
"detail": "Cannot autowire argument $data of "App\Controller\MyCustomController()": it references class "ApiPlatform\Doctrine\Orm\Paginator" but no such service exists.",
"status": 500,
"type": "/errors/500",
"trace": [
{
"file": "/app/vendor/symfony/dependency-injection/Argument/ServiceLocator.php",
"line": 40,
"function": "getService",
"class": "Symfony\Component\DependencyInjection\Container",
"type": "->"
}
]
}
From my understanding, ApiPlatformDoctrineOrmPaginator
is no longer a service, which is why it cannot be injected.
How can I access the Paginator in Api Platform 4? I need to use it in my custom controller. Is there a new recommended approach to achieve this?
1