首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony ApiPlatform自定义删除操作触发默认删除事件

Symfony ApiPlatform自定义删除操作触发默认删除事件
EN

Stack Overflow用户
提问于 2018-09-24 21:40:00
回答 1查看 2.5K关注 0票数 0

我有一个关于api平台自定义路由功能的问题。当尝试使用DELETE方法实现自定义路由时,会为http请求中的对象触发事件系统(由参数转换器找到):

代码语言:javascript
复制
* @ApiResource(
*     attributes={
*         "normalization_context": {
*         },
*         "denormalization_context": {
*         },
*     },
*     itemOperations={
*     },
*     collectionOperations={
*         "customObjectRemove": {
*             "method": "DELETE",
*             "path": "/objects/{id}/custom_remove",
*             "controller": CustomObjectRemoveController::class,

因此,即使我在控制器中编写了自己的逻辑,我的实体在api平台事件系统中也总是被触发删除。我怎样才能防止这种行为?

EN

回答 1

Stack Overflow用户

发布于 2019-05-15 10:42:23

您可以实现一个实现EventSubscriberInterface的事件订阅服务器:

代码语言:javascript
复制
<?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
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52480707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档