Changing properties of a route in Drupal 8


I want to change the title and controller of the '/user/reset/{uid}/{timestamp}/{hash}'

user reset route


First we need to  find away to alter the route. After building routes , the RoutingEvents::ALTER event triggers the route alter process. The \Drupal\Core\Routing\RouteSubscriberBase class contains an event listener that listens to this event. You can alter existing routes by implementing the alterRoutes(RouteCollection $collection) method of this class. You need to write you own implementation of the class like below



namespace Drupal\my_module\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 controller for user reset
    if ($route = $collection->get('user.reset')) {
      $route->setDefault('_controller', '\Drupal\my_module\Controller\MyModuleUserController::resetPass');
      $route->setDefault('_title', 'Reset your password');



After doing this we also need to register this service to make sure it is picked up. You need to add is as  a event_subscriber to you module's services.yml (in this case

  class: Drupal\my_module\Routing\RouteSubscriber
    - { name: event_subscriber }

Thats it , now clear the cache or enable the module. 


NOTE: If changing the _controller be extra cautious when doing so