Drupal routematch

Routematch is a Drupal 8/9 service that retrieves the currently active route match object. In simpler terms, it gives you more information about the current route. It's one of the most used services that Drupal offers, so let's see how to use it.

If you want to use this service in your .module or any other legacy procedural file you can't use dependency injection. You have to use \Drupal global class which acts as a global accessor to services:

$route_match = \Drupal::routeMatch();

The actual name for the service is current_route_match so you can also call it like this:

$route_match = \Drupal::service('current_route_match');

If you want to use it for example in a controller you can use dependency injection like this:

<?php

namespace Drupal\MY_MODULE\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Symfony\Component\DependencyInjection\ContainerInterface;

class MyController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\CurrentRouteMatch
   */
  protected $currentRouteMatch;

  /**
   * Constructs a new MyController object.
   *
   * @param \Drupal\Core\Routing\CurrentRouteMatch $currentRouteMatch
   *   The current route match.
   */
  public function __construct(CurrentRouteMatch $currentRouteMatch) {
    $this->currentRouteMatch = $currentRouteMatch;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('current_route_match')
    );
  }

Now that we have the service we can use it. The most basic usage is to check the current route name. For example, you may want to alter the page title block for some routes. You can do it like this:

/**
 * Implements hook_preprocess_HOOK().
 */
function MY_MODULE_preprocess_page_title(&$variables) {
  $route_match = \Drupal::routeMatch();
  if ($route_match->getRouteName() == 'view.frontpage.page_1') {
    $variables['title'] = t('New page title');
  }
}

As you can see the route name is just a string. In this case, we are checking if we are on the default front page view route. If you don't know the route name the easiest way to find it is to install the Devel module and then go to the '/devel/routes' page.

Drupal 8/9 - Devel routes
Drupal 8/9 - Devel routes

Sometimes getting just the route name is not enough. Usually, routes have parameters, and getting the current route parameters is just as easy as getting the route name.

For example the entity.node.canonical route has the following path:

/node/{node}

It's obvious that {node} is a route parameter. You can get this param by using the getParameter() method:

$route_match = \Drupal::routeMatch();
$node = $route_match->getParameter('node');

Variable $node will contain the Node object. If you want to get just the node ID use this:

$route_match = \Drupal::routeMatch();
$node_id = $route_match->getRawParameter('node');

A route might have more than one parameter. Routematch service can help you with that as well. You can get all route params by using getParameters() and getRawParameters() methods.

$route_match = \Drupal::routeMatch();
$params = $route_match->getParameters();

$route_match = \Drupal::routeMatch();
$params = $route_match->getRawParameters();

And finally, the last useful method I want to show you is getRouteObject(). This method will return the route object. I use this method very rarely, but it's good to know that it exists. It might come in handy in some situations.

$route_match = \Drupal::routeMatch();
$route = $route_match->getRouteObject();

The route variable will look something like this:

Drupal 8/9 - Route object
Drupal 8/9 - Route object

And that's all that I use from the routematch service. Not too many methods but they all are very useful in day-to-day coding.

About the Author

Goran Nikolovski is a creator, speaker, open-source contributor, web developer specialized in Drupal and DevOps enthusiast. He is the founder of this website and he enjoys sharing his knowledge and experiences.