Drupal Redirect by Symfony eventsubscriber

As drupal_goto is deprecated in drupal 8, we can still achieve this.

  • Create New module. e.g, myRedirectModule
  • add a service file. e.g, myRedirectModule.services.yaml
  • add a New php file file name should be same as class name and you already given in service file. e.g, ResponseSubscriber.php
  • In class ResponseSubscriber add some methods

    • public static function getSubscribedEvents()
      { $events[KernelEvents::RESPONSE][] = 'newRedirectUrl'; return $events; }

    • public function newRedirectUrl(\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) {
      $requestUri = $event->getRequest()->getRequestUri();  $event->setResponse(new RedirectResponse('urltoredirect', 301)); }