src/EventSubscriber/GlobalVariableSubscriber.php line 22

Open in your IDE?
  1. <?php 
  2. namespace App\EventSubscriber;
  3. use App\Repository\InformationRepository;
  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 GlobalVariableSubscriber implements EventSubscriberInterface
  9. {
  10.     private $twig;
  11.     private $informationRepository;
  12.     public function __construct(Environment $twigInformationRepository $informationRepository)
  13.     {
  14.         $this->twig $twig;
  15.         $this->informationRepository $informationRepository;
  16.     }
  17.     public function onKernelController(ControllerEvent $event)
  18.     {
  19.         // Ajoutez la variable globale "informationCount" à Twig
  20.         $this->twig->addGlobal('informationCount'$this->informationRepository->count([]));
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             KernelEvents::CONTROLLER => 'onKernelController',
  26.         ];
  27.     }
  28. }