H
ow to do something in a neater and smarter way is ideal for professional development. In programming one problem can be solved in many ways but using the better & recommended way is an asset to know about.
In this section we will show how to do the most important and useful tasks in Drupal development. These approaches are gathered from open source community and Drupal core.
Alter existing route
To alter an existing route, you need to create a RouteSubscriber class and add method alterRoutes as show below.
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
public function alterRoutes(RouteCollection $collection) {
// Change controller of '/admin/reports/dblog'
if ($route = $collection->get('dblog.overview')) {
$route->setDefault('_controller','\Drupal\modulename\Controller\MyController::overview');
}
}
}