44

The route name of the current page is available in page.html.twig? The page is generated by the default feedback form.

3 Answers 3

106

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']
2
  • 2
    There 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.2
    Commented Aug 21, 2019 at 16:33
  • @sea26.2 It use to be drupal router:debug :grin: but is now drupal debug:router Commented Dec 15, 2020 at 15:32
5

Another way to handle this is by using the Devel Module if you are developing locally.

  1. Download and enable Devel
  2. Log in as an administrator
  3. Navigate to /admin/config/development/devel/toolbar
  4. check "Current route info"
  5. click Save configuration
  6. Navigate to the url you are wanting the route information for
  7. Click Devel in your toolbar above the admin menu
  8. 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.

screen capture of enabling route details

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.