9

In Drupal 7, I use the following code.

function my_goto($path) { 
  drupal_goto($path, array(), 301);
}

What code should I use in Drupal 8?

  • 3
    drupal.org/node/2023537 is the change record for drupal_goto(), whenever you are looking for a replacement for a specific function, look there first. – Berdir Dec 3 '14 at 8:24
19

This is the code that should be used in Drupal 8. See change Record for more info.

use Symfony\Component\HttpFoundation\RedirectResponse;

function my_goto($path) { 
  $response = new RedirectResponse($path);
  $response->send();
  return;
}
8

To build on Anu Mathew's response;

To add a status code, its just the second param in the RedirectResponse class;

use Symfony\Component\HttpFoundation\RedirectResponse;

function my_goto($path) { 
  $response = new RedirectResponse($path, 302);
  $response->send();
  return;
}
4

I didn't work in drupal 8 yet but as per the documentation drupal_goto is removed from Drupal 8.

In place of drupal_goto you need to write:

return new RedirectResponse(\Drupal::url('route.name'));

and something like this with parameters :

return new RedirectResponse(\Drupal::url('route.name', [], ['absolute' => TRUE]));

Check here https://www.drupal.org/node/2023537 and class RedirectResponse

  • Thanks for your reply.But how can i get the route name from the current url(Because url is setting using configuration form) – Anu Mathew Dec 3 '14 at 5:48
  • You mean redirect path is dynamic??? – Sumit Madan Dec 3 '14 at 5:52
  • Yes, your right its dynamic.. – Anu Mathew Dec 3 '14 at 5:55
  • Ok try to replace \Drupal::url('route.name') with your url or perhaps absolute url. – Sumit Madan Dec 3 '14 at 6:03
  • return new RedirectResponse($absolute_url); is worked for me :) But its showing "redirect to example.com/absolute_url " message on the screen – Anu Mathew Dec 3 '14 at 6:15
4

This can be achieved by leveraging built-in symphonies EventDispatcher Component. All you have to do is create a custom module. Add your services.yml file and provide appropriate service config.

services:
  mymodue.subscriber:
    class: Drupal\my_module\EventSubscriber
    tags:
      - { name: event_subscriber }

in Your modules src directory add create your EventSubscriber.php class and describe you methods here.

    <?php
       use Symfony\Component\HttpFoundation\RedirectResponse;

        public function checkForCustomRedirect(GetResponseEvent $event) {     
            $route_name = \Drupal::request()->attributes->get(RouteObjectInterface::ROUTE_NAME);
            if($route_name === 'module.testPage') {
              $event->setResponse(new RedirectResponse($url, $status = 302,$headers);
            }
         }

          /**
           * {@inheritdoc}
           */
        public static function getSubscribedEvents() {
            $events = [];
            $events[KernelEvents::REQUEST][] = array('checkForCustomRedirect');
            return $events;
        }
1

Perfectly working redirect code for me is the following:

$response = new RedirectResponse($path);
return $response->send();

In any other cases I'm getting some kind of exceptions or errors, for example: LogicException: The controller must return a response...

OR

https://www.drupal.org/project/drupal/issues/2852657

There is already a discussion about it, hope that helps!

0

this works for internal or external redirection:

use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Url;

   $url = Url::fromUri('internal:/node/27'); // choose a path
   // $url =  Url::fromUri('https://external_site.com/');
    $destination = $url->toString();
    $response = new RedirectResponse($destination, 301);
    $response->send();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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