Redirect examples - Drupal 8

 


Ex:
use Symfony\Component\HttpFoundation\RedirectResponse;

// Redirect to specific route or URL.
return new RedirectResponse(\Drupal::url('user.page'));

return new RedirectResponse(\Drupal::url('locale.translate_status', [], ['absolute' => TRUE]));
	
return new RedirectResponse(\Drupal::url('<front>', [], ['absolute' => TRUE]));
	
return new RedirectResponse(Url::fromRoute('system.modules_uninstall')->setAbsolute()->toString());

return new RedirectResponse(Url::fromRoute('<current>')->toString());


// Redirect to external  URL.
use Drupal\Core\Routing\TrustedRedirectResponse;

return new TrustedRedirectResponse('http://www.google.com/');

// If you have extended ControllerBase class or use Drupal\Core\Routing\UrlGeneratorTrait;
return $this->redirect('user.login');

// If you have configurable URL
return new RedirectResponse(URL::fromUserInput('/user-input-url')->toString());

Comments

Popular posts from this blog

Programatically create layout builder section in Drupal

Code quality analysis of Drupal. Can I use Sonar?

Set up Drupal7 to Drupal8 migration in simple steps (using Drush)