src/EventSubscriber/CookieSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Cookie;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. class CookieSubscriber implements EventSubscriberInterface
  9. {
  10.     private $session;
  11.     public function __construct(SessionInterface $session)
  12.     {
  13.         $this->session $session;
  14.     }
  15.     
  16.     public function onRequestEvent(RequestEvent $event)
  17.     {
  18.         $cookieVisitorTagName 'visitor_tag';
  19.         if (!isset($_COOKIE[$cookieVisitorTagName])) {
  20.             $visitorTag uniqid();
  21.             setcookie($cookieVisitorTagName$visitorTag);
  22.             $_COOKIE[$cookieVisitorTagName] = $visitorTag;
  23.         }
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             RequestEvent::class => 'onRequestEvent',
  29.         ];
  30.     }
  31. }