Here's my code(using Drupal 8):

public function checkForRedirection(GetResponseEvent $event) {
    $is_authenticated = $this->currentUser->isAuthenticated();
    $route_name = $this->routeMatch->getRouteName();

    if (!$is_authenticated && $route_name !== "user.login") {
      header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
      header("Cache-Control: post-check=0, pre-check=0", FALSE);
      header("Pragma: no-cache");
      define("SITE", "www.google.com");
      $response = new RedirectResponse(SITE, $this->redirectCode);

   * {@inheritdoc}
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = ["checkForRedirection", 30];
    return $events;

So I'm redirecting an authenticated user away from my site. It works perfectly how I want it to, the only problem is PHP Mess Detector doesn't allow the exit(0) function, so wondering what are my alternatives to produce same functionality that exit(0) does here?

  • 3
    You need $event->setResponse($response);, see for example drupal.stackexchange.com/a/223109/47547. This stops the propagation of the event and finishes the Drupal kernel, even without the need for $event->stopPropagation(); which the code example is using.
    – 4uk4
    Commented Feb 29, 2020 at 9:42
  • Thank you so much!!! Commented Mar 2, 2020 at 21:23

1 Answer 1


You need to terminate using the kernel. You'll have to generate $currentRequest and $response, which may already be available depending on where your code lies. After you have them, you can terminate the request using:

\Drupal::service('kernel')->terminate($currentRequest, $response);
  • 1
    You can't terminate a symfony http kernel from the inside with this command. The terminate method of the kernel is called from the outside after the main run of the kernel has finished.
    – 4uk4
    Commented Feb 29, 2020 at 9:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.