In many cases, a route name is something you might need to get something done in Drupal 8. For example to generate a link, or maybe create a local task in your module.
Some examples:
The path can be found in a routing.yml file
Some times you can just search for the path in your codebase, and then find the corresponding route name. Let's say that I wanted to link to the page mysite.com/admin/config/regional/translate. If I just search the codebase for this path, it would be revealed in the file locale.routing.yml:
locale.translate_page:
path: '/admin/config/regional/translate'
defaults:
_controller: '\Drupal\locale\Controller\LocaleController::translatePage'
_title: 'User interface translation'
requirements:
_permission: 'translate interface'
To link to this page using the API for generating links, I would then do something like this:
$link = Link::fromTextAndUrl(t('Translate interface'), Url::fromRoute('locale.translate_page'));
So to conclude, the route name for that particular page is the key in the routing file, in this case locale.translate_page.
The path can not be found in a routing.yml file
Now, this is what I really wanted to write about in this blog post. Getting the route name directly from a routing file is simple enough, but where do you look if the path can not be found in a routing file?
Find route name with PHPStorm
My first trick is to utilize the IDE I use, PHPStorm.
Start by setting a breakpoint in index.php on the line that looks like this:
$response->send();
Next step, refresh your browser on the page you want to know the route name for, and hopefully trigger your breakpoint. Then you click on the icon for "evaluate expression". On my work computer this has the shortcut key alt-f8, but you can also find it in the debugger toolbar, or via the menu (Run -> Evaluate expression).
Then evaluate the following code:
\Drupal::routeMatch()->getRouteName()
That should give you the name of the route. As illustrated below in a gif:
Find route name with any development enviroment.
Now, I realize that not everyone uses PHPStorm, so here is one solution that should work without having xdebug and an IDE set up:
Following the same tactic as above, let's open up index.php again. Now, just change the following code:
$response = $kernel->handle($request);
+print_r(\Drupal::routeMatch()->getRouteName());
$response->send();
The difference here is adding the line with print_r.
Now visit the page you want to know the route name for. This will print the name of the route as the very first output of your Drupal site. Since you probably do not want this for your live Drupal site, this is best done on a development copy.
Find route name with Drupal Console
Drupal Console maintainer Jesus Manuel Olivas pointed out in the comments a rather cool way to browse the router list of routes:
drupal debug:router | peco | awk '{print $1}' | xargs drupal debug:router
This requires you to have Peco and Drupal console installed You can look at the animation of it in use here.
Other options
You can also use the module webprofiler which is a part of the devel module. This may or may not involve more steps than necessary, depending on your project. But to be fair, that is also an option.
To finish off, here is an animated gif in the category "route". Let me know your tips and tricks in the comments!
Max Bachhuber•Friday, Apr 20th 2018 (over 6 years ago)
I also found the drupal console helpful. There is a command called "debug:router" which lists all available routes and corresponding paths.
So you can do the following:
"drupal debug:router | grep node/"
This will show someting like this: https://pastebin.com/VYiPDCNa
Definitively simpler than the phpstorm debug method...
eiriksm•Friday, Apr 20th 2018 (over 6 years ago)
Good tip! Thanks for commenting!
The best use case of that approach is when you know some parts of the url so you can find it in the list. In the example of node/nid it might be fairly simple, but a route could consist of purely dynamic parts as well. With my approaches you can find any and all routes with no prior knowledge
Jesus Manuel Olivas•Sunday, Apr 22nd 2018 (over 6 years ago)
```
drupal debug:router | peco | awk '{print $1}' | xargs drupal debug:router
```
This is how it looks like: https://i.imgur.com/pnlzTDR.gif
eiriksm•Tuesday, Apr 24th 2018 (over 6 years ago)
Very cool!
Although I did not specifically talk about it in the article, using the route match service also makes it really easy to find the route parameters (which quite often are needed for creating a link).
But this was a really cool trick and tip! Hope you don't mind I add it to the article?
Also, thanks for drupal console! :)
Jesus Manuel Olivas•Tuesday, Apr 24th 2018 (over 6 years ago)
Feel free to add to the article ;)
Do you want to comment?
This article uses github for commenting. To comment, you can visit https://github.com/eiriksm/eiriksm.dev-comments/issues/15.