The route name of the current page is available in page.html.twig
?
The page is generated by the default feedback form.
3 Answers
To get the current route name, use:
$route_name = \Drupal::routeMatch()->getRouteName();
You can add the current page's route name as a variable in your theme's ".theme" file. Add a _preprocess_page function like this, and clear the drupal cache.
/**
* Implements hook_preprocess_page().
*
*/
function mytheme_preprocess_page(&$variables) {
$variables['route_name'] = \Drupal::routeMatch()->getRouteName();
}
Then you can access in page.html.twig like this:
{{ route_name }}
Note: \Drupal::routeMatch()->getRouteName()
will sometimes return null.
If you are inside a class, to do things properly, you'll want to inject the route match service in the constructor and then call it this way:
$this->currentRouteMatch->getRouteName()
The constructor (and variable) will be like this:
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $currentRouteMatch;
/**
* Constructs a new ThemeTestSubscriber.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
*/
public function __construct(RouteMatchInterface $current_route_match) {
$this->currentRouteMatch = $current_route_match;
}
If it's a service class, you'd then pass it into the service in the yaml file in your custom module:
services:
mymodule.service:
class: Drupal\mymodule\MyCustomService
arguments: ['@current_route_match']
-
2There is a CLI (line mode command) somewhere that shows all drupal routes... can't seem to remember what it was... I'll update (unless someone else knows)– sea26.2Aug 21, 2019 at 16:33
-
@sea26.2 It use to be
drupal router:debug
:grin: but is nowdrupal debug:router
Dec 15, 2020 at 15:32
Another way to handle this is by using the Devel Module if you are developing locally.
- Download and enable Devel
- Log in as an administrator
- Navigate to /admin/config/development/devel/toolbar
- check "Current route info"
- click Save configuration
- Navigate to the url you are wanting the route information for
- Click Devel in your toolbar above the admin menu
- Click Current route info
If you are in a theme or situation where you can't find the admin toolbar or can't find the "Current route info" menu link, modify the url /devel/routes/item?path=/user/1
where /user/1
is the path that you are looking for path information for. It appears that this method will not work for pages that change routes based on get parameters... for example, ?_format=json
or other custom routing like that.
Not for in a template, but to inspect the route: drush core:route
:
drush core:route --path=/node/123/latest
drush core:route --path=/user/1
Ref: drush commands docs: core:route
Example:
$ drush core:route --path=/node/123/latest
name: entity.node.latest_version
path: '/node/{node}/latest'
defaults:
_entity_view: node.full
_title_callback: '\Drupal\Core\Entity\Controller\EntityController::title'
requirements:
_entity_access: node.view
_content_moderation_latest_version: 'TRUE'
node: \d+
options:
_content_moderation_entity_type: node
parameters:
node:
type: 'entity:node'
load_latest_revision: true
converter: paramconverter.entity
_access_checks:
- access_check.entity
- access_check.latest_revision