kernel.view event not triggered even it's registered

161 views Asked by At

I'm creating a user system and would like to encrypt the password when I capture it via the POST method.

To do this, I've used Symfony's make:subscriber command to create my function.

Although the subscriber is registered :

 ------- ---------------------------------------------------------------------------------- ----------
  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
 ------- ---------------------------------------------------------------------------------- ----------

It's not triggered. I've tried deliberately setting a non-existent function and I don't get an error message either. So I think it's not being executed at all.

There is my code :

<?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],
        ];
    }
}

I want to execute the function and see my dd($entity) after my post request.

1

There are 1 answers

1
grobet On

Just found the reason, I needed to set the event_listeners_backward_compatibility_layer at true in the API platform configuration. Thanks a lot.