1

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();
  }
}
4
  • Does this help? drupal.stackexchange.com/questions/295056/…
    – mona lisa
    Commented Jun 10, 2022 at 15:19
  • I think it does, but the redirection is still applied when I refresh the destination...
    – lobafox
    Commented Jun 13, 2022 at 8:38
  • I think this question is the same as the other.
    – mona lisa
    Commented Jun 13, 2022 at 13:01
  • It looks the same, but unfortunatly, even with CacheableMetadata, it's still redirect to the wrong page
    – lobafox
    Commented Jun 13, 2022 at 13:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.