Problem/Motivation
There are two example modules which shows how to define routes: menu_example and page_example. They both show how to create routes associated to controllers.
Proposed resolution
Replace those modules with the controller_example module, which shows how controllers are associated to routes in Drupal 11.4.
Issue fork examples-3588616
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #6
avpadernoComment #7
oily commentedIn the existing menu_example (5.0.x branch), there is a local tasks set of routes that are dynamically generated. It is done using a callback defined at the bottom of the menu_example.routing.yml.
I have not found a way of defining the callback in an Route attribute in the controller.
I am looking for ideas on how to implement the multiple tabs (local tasks) example. I could:
Setting the issue to 'Needs review' for feedback on this issue.
Also, I deleted the menu_example.route_subscriber service and replaced it with a method inside the controller. But I think the service is worth keeping. Any ideas on whether to keep both examples or just one of them would be useful..
Also, there is a simple route example in the page_example module that I think should be copied over as it is truly simple. The first route example in the controller_example copied from menu_example is not as simple. Any ideas on whether to keep both or just one?
Comment #8
oily commentedComment #9
avpadernoI did not find any issue about using attributes for dynamic routes. They can still be added using a service tagged route_provider, which does not require entries in .routing.yml files. If there is already a service with that tag, it could be used for dynamic routes, or a new service can be added.
The menu_example.route_subscriber service is for altering existing routes. It should be kept in the example_controller module, after the necessary changes. (For example, by looking at Drupal core services, it does not seem that service should be tagged event_subscriber.)
Comment #10
oily commentedComment #11
oily commentedI have removed most of the test assertions except for the more complex routes. The Examples controller seems to be working for all the routes.
Comment #12
oily commentedRe: #9
I looked at the Drupal 12.0-dev core modules that implement dynamic routes. They all contain a reference to the dynamic routing callbacks in a *.routing.yml file. I tried referencing the dynamic routing class outside of a controller_example.routing.yml file, in a controller_example.services.yml file but I could not get that to work.
I believe the core image module is one of those that use a dynamic routing class, if anyone wants to look at it they can look at the image.routing.yml and the reference at the bottom of it.
Comment #13
avpaderno#3584791: Convert route callbacks to event subscribers explains how modules can add new routes without using the .routing.yml file. No Drupal core module is yet using that event subscriber type, since the issue which made that possible is recent (#3311365: Use PHP attributes for route discovery).
Comment #14
oily commentedReady for review again, I think.
Comment #15
oily commentedComment #16
oily commentedChanging to 'needs review' which is correct.
Comment #17
avpadernoComment #18
avpadernoComment #20
avpaderno