Here we are going to alter or change the default login (user/login) path.
- First, create a custom module if you don't know how refer to creating custom module documentations page.
- Create a RouteSubscriber inside your module create Routing folder inside src folder and inside it create RouteSubscriber class RouteSubscriber.php like the following
and put in it the following code:<?php namespace Drupal\YOUR_MODULE_NAME\Routing; use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Component\Routing\RouteCollection; /** * Listens to the dynamic route events. */ class RouteSubscriber extends RouteSubscriberBase { /** * {@inheritdoc} */ protected function alterRoutes(RouteCollection $collection) { // Change path '/user/login' to '/secrete/login'. if ($route = $collection->get('user.login')) { $route->setPath('/secrete/login'); } } }
-
Now we should register the below class as an event subscriber service, to do so:
Create a file in your root module folder called YOUR_MODULE_NAME.services.yml and put in it:
services: YOUR_MODULE_NAME.route_subscriber: class: Drupal\YOUR_MODULE_NAME\Routing\RouteSubscriber tags: - { name: event_subscriber }
- Now just clear the cache and you are done!
Comments