1

In the past I could set up redirects with drupal_goto using a simple path (i.e., node/1).

Is this not possible at all in Drupal 8? Everything I've seen relies on a route name but that requires either generating a page in a module or keeping a static note of a node id, which seems backwards.

If I use Url::fromUri(), I can't seem to use getRouteName() since it interprets that as an external URL (even though the domain is the Drupal install).

What is the best way to get the route name of an internal path?

2

drupal_goto is removed in favor of RedirectResponse (record).

As of Drupal 8 drupal_goto() has been removed in favor of RedirectResponse which comes together with the Symfony HttpFoundation.

hook_drupal_goto_alter() was also removed as well, in favor of just using an event listener on kernel.response

Drupal 7

<?php
function my_menu_callback_foo() {
  //....
  drupal_goto('user');
}
?>

Drupal 8 (procedural)

<?php
function my_menu_callback_foo() {
  $url = Url::createFromPath('node/1');
  return new RedirectResponse($url->toString());
 //Or try this:
 return new RedirectResponse('node/1');
}
?>
  • Correct but the problem with this is that you either need to create a named route and controller (bypassing the CMS portion of Drupal) or hard code the node id so you can call it using the entity.node.canonical route, which is never a good option. – Todd Bloom Dec 2 '14 at 16:58
  • See if the updated answer helps. – AyeshK Dec 2 '14 at 17:12
  • 2
    Hm, I don't see a createFromPath method on the Url object, might it have been removed? I'm using beta 2. I suppose hardcoding the ID will have to do; that's a shame. – Todd Bloom Dec 2 '14 at 19:48
  • Ah ha - using new RedirectResponse will allow an internal path and can be used with setRedirect(). Works for me! – Todd Bloom Dec 2 '14 at 20:03

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.