How to create a redirection in Drupal 8 ? By example, with hook, and after login or if an url is called.

$response = new Symfony\Component\HttpFoundation\RedirectResponse($url);
$response->send(); // don't send the response yourself inside controller and form.
  • 5
    You should not send the response yourself. If you are in a controller or a form (at least for now), just return the RedirectResponse object. – Berdir Feb 2 '15 at 19:16
  • Thank you for this information, my example is inside a hook, I have added a comment :) – Axel Briche Feb 3 '15 at 12:47
  • Also, if you need to redirect to another domain use \Drupal\Core\Routing\TrustedRedirectResponse – НЛО Dec 17 '15 at 21:36
  • Would you consider it ok adding an exit() after the redirection and skipping the return (in the context of language negotiation for example) ? – Bogdan Sep 7 '16 at 10:12

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