When a user (e.g. nid = 37) goes to some other user's profile page at a url like /user/1, /user/2, ..., /user/n, I want automatically redirect them to their own: /user/37 page.

How can I do properly this with Drupal 8?

I think I must create a custom Controller Class and configure the default drupal route (/user/{uid}) adding my custom Controller, but I don't know how can I do this.

1 Answer 1


There are at least two ways you could pursue to do what you're trying to achieve:

  1. You can alter routes using a RouteSubscriber.
  2. Listen for a event (likely KernelEvents::REQUEST), and respond appropriately.

The RouteSubscriber is probably a bit hard to do (I haven't written one yet, I just discovered their existence the other day), but is more in keeping with the older hook_menu_alter patterns from previous versions of Drupal.

The EventSubscriber is probably easier to write once you understand the event system (which requires some learning but is where lots of the future of Drupal internals is headed).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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