app/Customize/Controller/CommonEvent.php line 49

Open in your IDE?
  1. <?php
  2. /*--------------------------------------------------- */
  3. /* 共通変数を追加
  4. /*--------------------------------------------------- */
  5. namespace Customize\Controller;
  6. use Eccube\Request\Context;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Twig\Environment;
  11. class CommonEvent implements EventSubscriberInterface
  12. {
  13.     /** @var Context */
  14.     protected $requestContext;
  15.     /** @var Environment */
  16.     protected $twig;
  17.     /**
  18.      * Event constructor.
  19.      *
  20.      * @param Context $requestContext
  21.      * @param Environment $twig
  22.      */
  23.     public function __construct(
  24.         Context $requestContext,
  25.         Environment $twig
  26.     )
  27.     {
  28.         $this->requestContext $requestContext;
  29.         $this->twig $twig;
  30.     }
  31.     /**
  32.      * @return array
  33.      */
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             KernelEvents::REQUEST => ['onKernelRequest'100000000]
  38.         ];
  39.     }
  40.     /**
  41.      * @param GetResponseEvent $event
  42.      */
  43.     public function onKernelRequest(GetResponseEvent $event)
  44.     {
  45.         // 管理画面は除外
  46.         if ($this->requestContext->isAdmin()) {
  47.             return;
  48.         }
  49.         // ⭐️ ここで共通変数設定
  50.         // $this->twig->addGlobal('test', 'testだよ');
  51.     }
  52.     
  53. }