Problem
Here are some route names in Drupal 8 right now (taken from the Node module as an example) and their corresponding paths:
node.add_page:
path: '/node/add'
node.add:
path: '/node/add/{node_type}'
entity.node.version_history:
path: '/node/{node}/revisions'
node.revision_show:
path: '/node/{node}/revisions/{node_revision}/view'
There is no consistency or predictability at all.
Therefore, if I know the path that I want to link to (obviously a common occurrence) and I want to use Url::fromRoute() to do it, I need to stop what I'm doing and go look up the route name somewhere else.
Proposed fix
Change all route names so they use some kind of pattern that is easily-derivable from the canonical path. Anything is fine really, as long as it's consistent and easy to derive.
Impact/Disruption/Whatever
Disruption would be pretty high, I think.
The impact on developer experience could be pretty high as well. However the exact impact depends on the outcome of #2405551: Add a method to support UIs where users enter paths instead of route names and other valid use cases.
Comments
Comment #1
David_Rothstein CreditAttribution: David_Rothstein commentedComment #2
David_Rothstein CreditAttribution: David_Rothstein commentedComment #3
Wim LeersFYI: #2350503: Add route generation handlers for entities brought a big step forward in terms of having logical route names.
Comment #4
Wim LeersAnd #2350509: Implement auto-route generation for all core entities and convert all of the core entities. is the next step, actively being worked on.
Comment #5
David_Rothstein CreditAttribution: David_Rothstein commentedGood to hear :) That will help. Unfortunately it's not just about entities, though. For example, in System module:
So far so good (although the "system" part is not 100% obvious if you don't happen to remember which module controls it).
Sigh :(
Basically the inconsistencies seem to be almost everywhere, in one form or another.
Comment #6
Wim LeersIndeed :( There's no way for us to enforce things there. This is another reason paths were nice: it forced us to be logical and consistent.
Comment #7
effulgentsia CreditAttribution: effulgentsia commentedWould having a Drush command and/or Symfony console app that allows you to do something like:
drush show_route_name_and_parameters '/admin/config/media/file-system'
be sufficient? Or is this issue still needed regardless to support developers who don't use CLI? A web-based version via devel or similar module could also be made. Neither fully addresses the "I need to stop what I'm doing and go look up the route name somewhere else" problem, but what if that "somewhere else" were easy to get to?
Comment #8
David_Rothstein CreditAttribution: David_Rothstein commentedThat would definitely help, to have a way to look up the route name quickly. But yeah, I don't think it fully solves it. Being interrupted while in the middle of writing code is pretty annoying; it breaks flow, etc.
In Drupal 7 as an experienced site builder you can just do url('node/add') without thinking about it. Or even if you're not experienced enough using Drupal to have the URLs memorized, at least if you're linking to a page it's probably because you just visited that page in your web browser, so the URL's right there in front of you in short term memory anyway :)
Comment #9
webchickI think the DX benefits of doing this are pretty significant, so raising to major.
Comment #10
pwolanin CreditAttribution: pwolanin commentedI agree with effulgentsia - though I envisioned having a little helper widget on the admin toolbar.
imho - this should be a "won't fix"
Comment #11
dawehnerI think the DX would be horrible, because this is just magic.
Note: One thing which already helps a lot is that the webprofiler module provides you a toolbar which shows you the route name of the current request.
Comment #12
pwolanin CreditAttribution: pwolanin commentedCan we add some version of that toolbar to core?
Comment #13
effulgentsia CreditAttribution: effulgentsia commentedAn advantage of the above route name not containing the path structure is that in D9, we can move it from /admin/config/media/ to /admin/structure/media/, or whatever other organization might make sense at that time, without changing the route name. So one less thing to break for modules porting from 8 to 9.
Looking through system.routing.yml, I actually see pretty good consistency of route names containing only the minimum words needed to describe what function the page serves rather than where in the path structure it happens to be for this particular Drupal version. The only exceptions that jump out at me are the
system.admin_config_*
route names, but those are listing pages for a particular subgroup, so I don't have a suggestion for better names.Comment #14
David_Rothstein CreditAttribution: David_Rothstein commented@pwolanin, do you think this should be "won't fix" because of the disruption it would cause at this point in the release cycle, or some other reason?
This is true; however I think a one-time replacement when converting a module from Drupal N to Drupal N+1 (which is essentially just a search+replace) is a small price to pay for a better developer experience throughout the Drupal N cycle :)
Comment #15
pwolanin CreditAttribution: pwolanin commented@David_Rothsetin - as machine names I think the route names should reflect the module that provides them, and otherwise have descriptive names, rather than mimicking the path.
Comment #16
Crell CreditAttribution: Crell at Palantir.net commentedConcur with pwolanin. $module_name.$something_meaningful is the convention. A 1:1 mapping to the path is counter-productive, as we DO want those to be distinct.
Is this a won't fix at this point? I'm voting yes...
Comment #17
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedURLs are meaningful (at least one hopes!) so basing the route name off the canonical path would meet the above criteria.
Unfortunately this issue probably needs to be bumped to Drupal 9 at this point.
Comment #18
pwolanin CreditAttribution: pwolanin as a volunteer and at Acquia commentedyes, seems so.
At this point I'm not sure adopting any of the symfony routing was actually a good idea, but I also don't think we are going to feasibly rip it out now or in 9, and renaming lots of routes in 9 doesn't seems like it's going to be that useful
Comment #19
catchComment #21
xjmFiddling with route names is a pretty big disruption, so this would be a minor-only change and would need some sort of BC... Since 8.9.x and 9.0.x are now in beta, I'm moving this to 9.1.x. It's not postponed on anything specific, so setting to active. Thanks!