Drupal 11.4 Introduces PHP Attribute-Based Routing for Controllers
Drupal 11.4.0 introduces support for defining routes directly within controller classes using PHP attributes, marking a change in how routing configuration can be handled in Drupal core. The update allows developers to declare routes alongside controller methods instead of relying solely on separate .routing.yml files.
The feature enables the use of the #[Route(...)] attribute to specify route paths, names, defaults, and requirements directly in controller code. Classes within a module’s Controller namespace are automatically discovered, allowing attribute-defined routes to be registered without separate YAML configuration.
The implementation supports both class-level and method-level attributes. Defaults, requirements, and options defined at the class level can be extended or overridden at the method level, while route names and paths can be composed through prefixing. This allows multiple routes to be defined within a single controller class with shared configuration.
The update remains fully backward compatible, with existing routing definitions in .routing.yml continuing to function. This allows teams to adopt attribute-based routing incrementally without disrupting existing applications.
The change record for this update documents the feature and its intended use within Drupal 11.4.0.
