kernel.view 事件即使已注册也未触发

问题描述 投票:0回答:1

我正在创建一个用户系统,并希望在通过 POST 方法捕获密码时对密码进行加密。

为此,我使用 Symfony 的 make:subscriber 命令来创建我的函数。

虽然订阅者已注册:

 ------- ---------------------------------------------------------------------------------- ----------
  Order   Callable                                                                           Priority
 ------- ---------------------------------------------------------------------------------- ----------
  #1      ApiPlatform\Symfony\EventListener\ValidateListener::onKernelView()                 64
  #2      ApiPlatform\Symfony\EventListener\DenyAccessListener::onSecurityPostValidation()   63
  #3      App\EventSubscriber\PasswordHasherSubscriber::hashPassword()                       33
  #4      ApiPlatform\Symfony\EventListener\WriteListener::onKernelView()                    32
  #5      ApiPlatform\Symfony\EventListener\SerializeListener::onKernelView()                16
  #6      ApiPlatform\Symfony\EventListener\RespondListener::onKernelView()                  8
  #7      Symfony\Bridge\Twig\EventListener\TemplateAttributeListener::onKernelView()        -128
 ------- ---------------------------------------------------------------------------------- ----------

未触发。我尝试故意设置一个不存在的函数,但也没有收到错误消息。所以我认为它根本没有被执行。

这是我的代码:

<?php

namespace App\EventSubscriber;

use ApiPlatform\Symfony\EventListener\EventPriorities;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class PasswordHasherSubscriber implements EventSubscriberInterface
{
    public function hashPassword(ViewEvent $event): void
    {
        $entity = $event->getControllerResult();
        $method = $event->getRequest()->getMethod();
        dd($entity);
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::VIEW => ['hashPassword', EventPriorities::PRE_WRITE],
        ];
    }
}

我想执行该函数并在发布请求后查看我的 dd($entity) 。

php symfony api-platform.com symfony-eventdispatcher
1个回答
0
投票

刚刚找到原因,需要在API平台配置中将event_listeners_backward_compatibility_layer设置为true。非常感谢。

© www.soinside.com 2019 - 2024. All rights reserved.