src/EventSubscriber/SeasonSubscriber.php line 26

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\SeasonService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Twig\Environment;
  8. class SeasonSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private SeasonService $seasonService,
  12.         private Environment $twig
  13.     ) {
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             KernelEvents::CONTROLLER => 'onKernelController',
  19.         ];
  20.     }
  21.     public function onKernelController(ControllerEvent $event): void
  22.     {
  23.         if (!$event->isMainRequest()) {
  24.             return;
  25.         }
  26.         // Přidáme globální proměnné do Twig pouze pro admin sekci
  27.         $request $event->getRequest();
  28.         $path $request->getPathInfo();
  29.         
  30.         if (str_starts_with($path'/admin')) {
  31.             $this->twig->addGlobal('allSeasons'$this->seasonService->getAllSeasons());
  32.             $this->twig->addGlobal('selectedSeason'$this->seasonService->getSelectedSeason());
  33.         }
  34.     }
  35. }