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);
$response->send();
$exit(0);
}
}
/**
* {@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?
$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. – 4k4 Feb 29 '20 at 9:42