In Drupal 7 we could register paths using hook_menu and alter those paths using hook_menu_alter but in Drupal 8 paths are registered via routing.yml files. To alter existing routes in Drupal 8 we need to create a service that extends RouteSubscriberBase. Below is how to do it with examples of how to disable the user registration page and user password reset page.
Create the service
module_name.route_subscriber:
class: Drupal\dm\RouteSubscriber
arguments: []
tags:
- { name: event_subscriber }
Create the RouteSubscriber.php Class
<?php
/**
* @file
* Alter existing routes.
*/
namespace Drupal\module_name;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Class RouteSubscriber.
*
* @package Drupal\module_name
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
public function alterRoutes(RouteCollection $collection) {
// Disable access to the registration page.
if ($route = $collection->get('user.register')) {
$route->setRequirement('_access', 'FALSE');
}
// Remove user password reset page.
if ($route = $collection->get('user.pass')) {
$route->setRequirement('_access', 'FALSE');
}
}
}