src/EventSubscriber/ResponseSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  5. use Symfony\Component\Security\Core\Security;
  6. class ResponseSubscriber implements EventSubscriberInterface
  7. {
  8.     public function __construct(private Security $security)
  9.     {
  10.     }
  11.     /**
  12.      * @param ResponseEvent $event
  13.      *
  14.      * @psalm-suppress PossiblyNullArgument
  15.      */
  16.     public function onKernelResponse(ResponseEvent $event)
  17.     {
  18.         if (!$event->isMainRequest()) {
  19.             return;
  20.         }
  21.         $event->getResponse()->headers->set('User-Id'$this->security->getUser()?->getUserIdentifier());
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             'kernel.response' => 'onKernelResponse',
  27.         ];
  28.     }
  29. }