我使用 Insomnia 软件通过 Symfony 7.1.7 和 API Platform 4.0.8 测试我的 API。
当我发送请求时,出现以下错误消息:
The service "App\EventListener\DeserializeListener" has a dependency on a non-existent service "api_platform.listener.request.deserialize".
我的 DeserializeListener.php:
<?php
namespace App\EventListener;
use ApiPlatform\Symfony\EventListener\DeserializeListener as DecoratedListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class DeserializeListener
{
public function __construct(private DecoratedListener $decorated)
{
}
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
if($request->isMethodCacheable() || $request->isMethod(Request::METHOD_DELETE))
{
return;
}
dd($request->getContent());
}
}
我的服务.yaml:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\OpenApi\OpenApiFactory:
decorates: 'api_platform.openapi.factory'
arguments: ['@App\OpenApi\OpenApiFactory.inner']
autoconfigure: false
App\StateProvider\DependenciesStateProvider:
arguments:
$rootPath: '%kernel.project_dir%'
App\StateProcessor\DependenciesStateProcessor:
arguments:
$rootPath: '%kernel.project_dir%'
App\EventListener\DeserializeListener:
tags:
- {
name: 'kernel.event_listener',
event: 'kernel.request',
method: 'onKernelRequest',
priority: 2
}
autoconfigure: false
decorates: 'api_platform.listener.request.deserialize'
arguments:
$decorated: '@App\EventListener\DeserializeListener.inner'
当我对“api_platform.listener.request.deserialize”使用自动完成功能时,图标为灰色。
我遵循的教程有点太旧了。
我尝试放一些其他东西来“装饰”,但我真的不知道该选择什么。
如何解决问题?
在文件 api_platform.yaml 中添加此行 use_symfony_listeners: true