Iam using a RedirectResponse in drupal 8 in my custom module when a user is logging in,

$user = User::load(\Drupal::currentUser()->id());
$userRoles = $user->getRoles();
   $response = new RedirectResponse('/home');

later i was trying to change the url but even after removing the code the redirection occurs.


1 Answer 1


Doing a bit of guessing here because your code snippet doesn't show where you've implemented this bit of code. However, just blanket sending a response in the middle of Drupal's runtime is probably always going to have some odd side effects. Symfony's routing system (which Drupal uses) provides a number of different layers at which you can get involved with the events deciding what response to send.

That being said, I don't think you need to worry about any of that. Drupal supports redirecting on user login already through a different mechanism. Something similar to what's outlined here would work well: https://www.drupal.org/forum/support/module-development-and-code-questions/2013-08-18/how-to-redirect-user-after-login-in#comment-11931382

I also found a previous stackexchange for this here: Redirect after user login

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