我有一个关于api平台自定义路由功能的问题。当尝试使用DELETE方法实现自定义路由时,会为http请求中的对象触发事件系统(由参数转换器找到):
* @ApiResource(
* attributes={
* "normalization_context": {
* },
* "denormalization_context": {
* },
* },
* itemOperations={
* },
* collectionOperations={
* "customObjectRemove": {
* "method": "DELETE",
* "path": "/objects/{id}/custom_remove",
* "controller": CustomObjectRemoveController::class,因此,即使我在控制器中编写了自己的逻辑,我的实体在api平台事件系统中也总是被触发删除。我怎样才能防止这种行为?
发布于 2019-05-15 10:42:23
您可以实现一个实现EventSubscriberInterface的事件订阅服务器:
<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;
final class DeleteEntityNameSubscriber implements EventSubscriberInterface
{
public function __construct()
{
// Inject the services you need
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['onDeleteAction', EventPriorities::PRE_WRITE]
];
}
public function onDeleteAction(GetResponseForControllerResultEvent $event)
{
$object = $event->getControllerResult();
$request = $event->getRequest();
$method = $request->getMethod();
if (!$object instanceof MyEntity || Request::METHOD_DELETE !== $method) {
return;
}
// Do you staff here
}
}https://stackoverflow.com/questions/52480707
复制相似问题