Can't find cookie for validation in EventSubscriber There's this module Splash Redirect in which src/EventSubscriber/SplashRedirectEventSubscriber.php is supposed to check if the cookie $config_cookie exists or not, if it already exists then the user continues onto the requested node ($config_source), but if it does not exist then the user is sent to $config_destination and it creates the cookie ($config_cookie).

It succeeds in creating the $config_cookie and the redirect to $config_destination but it never seems to acknowledge the existence of the cookie once created and continues to redirect the user to $config_destination.

Any clues? On Drupal 8.6

&lt;?php

namespace Drupal\splash_redirect\EventSubscriber;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Routing\TrustedRedirectResponse;

/**
 * Splash redirect Event Subscriber.
 */
class SplashRedirectEventSubscriber implements EventSubscriberInterface {

  /**
   * Triggered when system sends response.
   */
  public function modifyIntercept(GetResponseEvent $event) {
    $config = \Drupal::config('splash_redirect.settings');
    $config_enabled = $config-&gt;get('splash_redirect.is_enabled');
    $config_source = $config-&gt;get('splash_redirect.source');
    $config_destination = $config-&gt;get('splash_redirect.destination');
    $config_cookie = $config-&gt;get('splash_redirect.cookie_name');
    $config_duration = $config-&gt;get('splash_redirect.duration');

    // If splash config is not enabled then we don't need to do any of this.
    if ($config_enabled == 1) {
      // Current request from client.
      $request = \Drupal::request();
      $current_uri = $request-&gt;getRequestUri();
      $http_host = $request-&gt;getHost();
      // Current response from system.
      $response = $event-&gt;getResponse();
      $route = (\Drupal::routeMatch()-&gt;getParameter('node')) ? \Drupal::routeMatch()-&gt;getParameter('node')-&gt;id() : null;
      $has_cookie = $request-&gt;cookies-&gt;has($config_cookie);

      // If splash-cookie has not been set, and user requesting 'source' page,
      // set cookie and redirect to splash page.
      if ($config_source == $route) {
        // Kill cache on this route or else cookie might not be read with VCL.
        \Drupal::service('page_cache_kill_switch')-&gt;trigger();

        if (!$has_cookie) {
          // Set redirect response with cookie and redirect location.
          $redir = new TrustedRedirectResponse($config_destination, '302');
          $cookie = new Cookie($config_cookie, 'true', strtotime('now + ' . $config_duration . 'days'), '/', '.' . $http_host, false, true);
          $redir-&gt;headers-&gt;setCookie($cookie);
          $redir-&gt;headers-&gt;set('Cache-Control', 'public, max-age=0');
          $redir-&gt;addCacheableDependency($config_destination);
          $event-&gt;setResponse($redir);
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    // Listen for response event from system and intercept.
    $events[KernelEvents::REQUEST][] = ['modifyIntercept'];
    return $events;
  }

}
Some ideas:

Like for the config you have to add a cache dependency for the cookie, too:

  $response-&gt;getCacheableMetadata()-&gt;addCacheContexts(['cookies:' . $config_cookie]);


Or use an uncacheable RedirectResponse to simplify things.

And try to set a priority for the event subscriber, for example 31, because your code depends only on routing, which has 32:

  $events[KernelEvents::REQUEST][] = ['modifyIntercept', 31];


You don't need to get a response in an request subscriber, because there isn't one yet. Get the request instead and make sure that you are in a master request. So this would be how the subscriber starts most times:

  public function onKernelRequest(GetResponseEvent $event) {
    if (!$event-&gt;isMasterRequest()) {
      return;
    }
    $request = $event-&gt;getRequest();
    ...




Simplifying a bit more and using dummy values for testing:

  public function onKernelRequest(GetResponseEvent $event) {
    if (!$event-&gt;isMasterRequest()) {
      return;
    }
    $route_match = \Drupal::routeMatch();
    if (!$route_match-&gt;getRouteName() == 'entity.node.canonical') {
      return;
    }
    $request = $event-&gt;getRequest();
    if ($route_match-&gt;getRawParameter('node') == '1') {
      \Drupal::service('page_cache_kill_switch')-&gt;trigger();
      if (!$request-&gt;cookies-&gt;has('foo')) {
        $redirect = new RedirectResponse($request-&gt;getBasePath() . '/node/2', 302);
        $redirect-&gt;headers-&gt;setCookie(new Cookie('foo', '123', '+30 seconds'));
        $event-&gt;setResponse($redirect);
      }
    }
  }


This is working on my local dev environment.



Complete code (including dependency injection):

/src/EventSubscriber/MymoduleSubscriber.php

&lt;?php

namespace Drupal\mymodule\EventSubscriber;

use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Cookie;

/**
 * mymodule event subscriber.
 */
class MymoduleSubscriber implements EventSubscriberInterface {

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs event subscriber.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this-&gt;routeMatch = $route_match;
  }

  /**
   * Kernel request event handler.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   Response event.
   */
  public function onKernelRequest(GetResponseEvent $event) {
    if (!$event-&gt;isMasterRequest()) {
      return;
    }
    if (!$this-&gt;routeMatch-&gt;getRouteName() == 'entity.node.canonical') {
      return;
    }
    $request = $event-&gt;getRequest();
    if ($this-&gt;routeMatch-&gt;getRawParameter('node') == '1') {
      \Drupal::service('page_cache_kill_switch')-&gt;trigger();
      if (!$request-&gt;cookies-&gt;has('foo')) {
        $redirect = new RedirectResponse($request-&gt;getBasePath() .'/node/2', 302);
        $redirect-&gt;headers-&gt;setCookie(new Cookie('foo', '123', '+30 seconds'));
        $event-&gt;setResponse($redirect);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::REQUEST =&gt; ['onKernelRequest', 31],
    ];
  }

}


mymodule.services.yml

services:
  mymodule.event_subscriber:
    class: Drupal\mymodule\EventSubscriber\MymoduleSubscriber
    arguments: ['@current_route_match']
    tags:
      - { name: event_subscriber }