src/EventSubscriber/ControllerEventSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\ActivityLogDataEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class ControllerEventSubscriber implements EventSubscriberInterface
  9. {
  10.     private EventDispatcherInterface $dispatcher;
  11.     public function __construct(EventDispatcherInterface $dispatcher)
  12.     {
  13.         $this->dispatcher $dispatcher;
  14.     }
  15.     public function onKernelController(ControllerEvent $event): void
  16.     {
  17.         $request $event->getRequest();
  18.         // Optional: Add conditions to control when to dispatch the custom event
  19.         if ($request->attributes->get('_route') === 'specific_route') {
  20.             $data = [
  21.                 'controller' => get_class($event->getController()),
  22.                 'route' => $request->attributes->get('_route'),
  23.                 'timestamp' => time(),
  24.             ];
  25.             // Dispatch the custom event
  26.             $activityLogEvent = new ActivityLogDataEvent($data);
  27.             $this->dispatcher->dispatch($activityLogEvent'activity_log.data_event');
  28.         }
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::CONTROLLER => 'onKernelController',
  34.         ];
  35.     }
  36. }