<?php
namespace App\EventSubscriber;
use App\Entity\Settings;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
class TwigEventSubscriber implements EventSubscriberInterface
{
private $twig;
private $entityManager;
public function __construct(Environment $twig, EntityManagerInterface $entityManager)
{
$this->twig = $twig;
$this->entityManager = $entityManager;
}
public function onKernelController(ControllerEvent $event)
{
// Hole die Settings aus der Datenbank
$settings = $this->entityManager->getRepository(Settings::class)->find(1);
// Übergibt die Settings an alle Twig-Templates
$this->twig->addGlobal('settings', $settings);
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}