I am considering the following approach in my controller (the method which corresponds to a given route).

$destination = \Drupal::destination()->getAsArray();
if (isset($destination['destination'])) {
  return new RedirectResponse(Url::fromUri($destination['destination']));
else {
  return $this->redirect('acquia_connector.settings');

Should I use different code to handle both route based and URL based redirects?

  • Edit - updated to make it clear that I'm looking for the Drupal 8 approach. – Balazs Dianiska Jan 27 '16 at 10:42

Drupal automatically uses destination, you don't have to do anything. If a destination argument is given, it will override default. So you can simplify your code to a simple, hardcoded $this->redirect(...).

The only exception is when you have links that you want to respect destination too (e.g. a cancel link on a confirm form), then you have to do it yourself. But the default confirm form already does that too AFAIK.

  • Could you point me to where is this destination override handled? I've been looking into ControllerBase::redirect, which invokes $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options), which looks up the url_generator service and invokes the relevant generateFromRoute method, which is this one. But in this method I can't find the override either. – Balazs Dianiska Jan 28 '16 at 7:45
  • Drupal\Core\EventSubscriber\RedirectResponseSubscriber is where this happens. – Berdir Jan 28 '16 at 14:23

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.