Twig Global
<?php
namespace App\Twig;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;
use Doctrine\Common\Collections\Criteria;
class TwigGlobalSubscriber implements EventSubscriberInterface
{
private $twig, $em;
public function __construct(Environment $twig, EntityManagerInterface $em)
{
$this->twig = $twig;
$this->em = $em;
}
public function injectGlobalVariables()
{
$tab = [];
foreach ($this->em->getRepository('App:Parametres')->findAll() as $parametre) {
$tab[$parametre->getNom()] = $parametre->getValeur();
}
$this->twig->addGlobal('parametres', $tab);
}
public static function getSubscribedEvents()
{
return [KernelEvents::CONTROLLER => 'injectGlobalVariables'];
}
public function onKernelRequest()
{
}
}
and in services.yml
App\Twig\TwigGlobalSubscriber:
tags:
- { name: kernel.event_listener, event: kernel.request }
cadot.eu