I'm trying to simply redirect to a certain page after is visited.
The scenario : the user click on a link. If the user a certain data (a store, for drupal commerce), he can go to my node. If not, redirect him to a middle page and after he submits a form, redirect him to my node.
It only works when the cache is cleared. I can't figure out why... even in private mode...
public static function getSubscribedEvents()
{
$events[KernelEvents::REQUEST][] = ['redirectToStore'];
return $events;
}
public function redirectTo(RequestEvent $event)
{
...
$path = Url::fromRoute('route.my_destination');
$response = new RedirectResponse($path->toString());
$url = \Drupal\Core\Url::fromRoute('<current>');
$host = \Drupal::request()->getSchemeAndHttpHost();
$session = \Drupal::request()->getSession();
//saving the goTo works only once, then I have to clean my cache, even in private
user_cookie_save(['goToHome' => $host . $url->toString()]);
//\Drupal::request()->cookies->set('goToHome', $host . $url->toString());
// $session->set('goToHome', $host . $url->toString());
// $_SESSION['goToHome'] = $host . $url->toString();
$event->setResponse($response);
//exemple : firefox 1 -> /my-target, redirect to /my-destination (url redirect in session), do stuff, redirect to /my-target
// firefox 2 -> /my-target, redirect to /my-destination (url in session doesn't exists), do stuff, redirect to home (instead of /my-target)
//I have to do this to make it workss
// \Drupal::service('page_cache_kill_switch')->trigger();
}
}