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

Command icon 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

avpaderno created an issue. See original summary.

oily made their first commit to this issue’s fork.

avpaderno’s picture

Status: Active » Needs work
oily’s picture

In 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:

  1. Create a separate example module that includes a *.routing.yml file with the callback defined.
  2. Use a controller_example.routing.yml file containing only the callback definition.
  3. If it is possible? to apply a Route attribute to the controller class that sets the callback.

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?

oily’s picture

Status: Needs work » Needs review
avpaderno’s picture

I 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.)

oily’s picture

Status: Needs review » Needs work
oily’s picture

Status: Needs work » Needs review

I have removed most of the test assertions except for the more complex routes. The Examples controller seems to be working for all the routes.

oily’s picture

Re: #9

I 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.

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.

avpaderno’s picture

#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).

oily’s picture

Ready for review again, I think.

oily’s picture

Status: Needs review » Reviewed & tested by the community
oily’s picture

Status: Reviewed & tested by the community » Needs review

Changing to 'needs review' which is correct.

avpaderno’s picture

Status: Needs review » Needs work
avpaderno’s picture

Status: Needs work » Reviewed & tested by the community

  • avpaderno committed f19378b7 on 5.0.x authored by oily
    Issue #3588616: Replace the menu_example and the page_example modules...
avpaderno’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.