Event Subscribers in Drupal 8 is great, which allow various system components to interact and communicate with one another while remaining independent, or decoupled. Drupal core dispatches many events on every request while other components can subscribe to these dispatched events.
In this post I will show how you can use the Drupal Event Subscriber to set a redirect. We can use the Redirect module to redirect users to a different page when a particular page is requested. But using a module when you need only a couple of redirections seems an overkill.
The use case here is, I need to redirect to a custom node when an anonymous user visits the registration page.
First we need define a service tagged with event_subscriber in your
Next, we need to define the
SignUpRedirect class in
src/EventSubscriber/SignUpRedirect.php file which implements the
So, what we are doing is on a page request, defined by the KernelEvents constant
REQUEST, we are checking for the current path. If the current path is the user register page we are redirecting the user to a custom page setting a