Problem/Motivation
Drupal should use modern PHP attributes for route discovery ala Symfony.
This will allow us to add an attribute to a method name and use that for routing information. For example:
/**
* The default 401 content.
*
* @return array
* A render array containing the message to display for 401 pages.
*/
#[Route(
path: '/system/401',
name: 'system.401',
requirements: ['_access' => 'TRUE'],
defaults: ['_title' => 'Unauthorized']
)]
public function on401() {
return [
'#markup' => $this->t('Please log in to access this page.'),
];
}
will replace the following entry in system.routing.yml
system.401:
path: '/system/401'
defaults:
_controller: '\Drupal\system\Controller\Http4xxController:on401'
_title: 'Unauthorized'
requirements:
_access: 'TRUE'
What are the advantages of doing this?
- The defaults._controller value is determined by the method you add the attribute to
- The route definition and the controller code live side by side
- No longer need route names - these can be automatically provided based on the class and method name
- Less difference between modern Symfony and Drupal
What are the disadvantages?
- The routing.yml file can give a good overview of what a module does and makes reviewing route access a bit simpler
Mitigations of the disadvantages:
- Currently the code is scanning all the classes in a module's src/Controllers directory for attributed methods - therefore it's not that difficult to scan this code and you benefit from the controller and route definition being together.
- We should and can build CLI tools to expose all the routes provided by a module. This would be good not just for routes determined by attributes but also dynamic routes
Proposed resolution
- Remove yaml discovery from the RouteBuilder into it's own service
- Add a new static routing event for the yaml discovery to listen to
- Add a new service for PHP attribute discovery and listen to the same event. This reflects on all classes in a module's src/Controllers directory to find routes
Remaining tasks
- Add more test coverage
- Decide which of the Symfony route features to keep in - env, localized_paths, priority, _locale, _canonical_route stuff... not sure what to do with this.
User interface changes
None
API changes
TBD
Data model changes
None
Release notes snippet
@todo