src/EventSubscriber/TwigEventSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Settings;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Twig\Environment;
  9. class TwigEventSubscriber implements EventSubscriberInterface
  10. {
  11.     private $twig;
  12.     private $entityManager;
  13.     public function __construct(Environment $twigEntityManagerInterface $entityManager)
  14.     {
  15.         $this->twig $twig;
  16.         $this->entityManager $entityManager;
  17.     }
  18.     public function onKernelController(ControllerEvent $event)
  19.     {
  20.         // Hole die Settings aus der Datenbank
  21.         $settings $this->entityManager->getRepository(Settings::class)->find(1);
  22.         // Übergibt die Settings an alle Twig-Templates
  23.         $this->twig->addGlobal('settings'$settings);
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::CONTROLLER => 'onKernelController',
  29.         ];
  30.     }
  31. }