订阅者在 API Platform 3.2 / Symfony6.4 中不会被调用

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

当使用 POST api 调用创建用户时,我让订阅者对密码进行哈希处理:

readonly class PasswordHashSubscriber implements EventSubscriberInterface
{
    public function __construct(private UserPasswordHasherInterface $userPasswordHasher)
    {
    }
    
    /**
     * @inheritDoc
     */
    public static function getSubscribedEvents(): array
    {
        return [
             KernelEvents::VIEW => ['hashPassword', EventPriorities::PRE_WRITE
            ],
        ];
    }

    public function hashPassword(ViewEvent $event): void
    {
        $user = $event->getControllerResult();
        $method = $event->getRequest()->getMethod();

        if (!$user instanceof User || Request::METHOD_POST !== $method) {
            return;
        }

        $user->setPassword($this->userPasswordHasher->hashPassword($user, $user->getPassword()));
    }
}

用户被创建并保留,但密码是“已提交的”并且不会被散列。我还放置了一些记录器,发现订阅者根本没有被调用。

订阅者似乎配置正确:

bin/console debug:container PasswordHashSubscriber                                    

Information for Service "App\EventSubscriber\PasswordHashSubscriber"
====================================================================

 ---------------- -------------------------------------------- 
  Option           Value                                       
 ---------------- -------------------------------------------- 
  Service ID       App\EventSubscriber\PasswordHashSubscriber  
  Class            App\EventSubscriber\PasswordHashSubscriber  
  Tags             kernel.event_subscriber                     
  Public           no                                          
  Synthetic        no                                          
  Lazy             no                                          
  Shared           yes                                         
  Abstract         no                                          
  Autowired        yes                                         
  Autoconfigured   yes                                         
  Usages           debug.event_dispatcher                      
 ---------------- -------------------------------------------- 


bin/console debug:event-dispatcher

 ------- ----------------------------------------------------------------------------- ---------- 
  Order   Callable                                                                      Priority  
 ------- ----------------------------------------------------------------------------- ---------- 
  #1      App\EventSubscriber\PasswordHashSubscriber::hashPassword()                    33        
  #2      Symfony\Bridge\Twig\EventListener\TemplateAttributeListener::onKernelView()   -128      
 ------- ----------------------------------------------------------------------------- ---------- 

我已经尝试过

event_listeners_backward_compatibility_layer: true
但没有运气

php symfony doctrine-orm api-platform.com symfony6
1个回答
0
投票

尽管这不是使用 api 平台设置用户密码的最佳方法(最好使用状态处理器),但您的订阅者没有被解雇的原因可能是因为在 api_platform.yaml 中,

use_symfony_listeners
设置为
false

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