current_route_match service

current_route_match

Documentation

\Drupal\Core\Routing\RouteMatchInterface

Container Injection

$container->get('current_route_match')

Direct Use

/** @var \Drupal\Core\Routing\RouteMatchInterface $currentRouteMatchService */
$currentRouteMatchService = \Drupal::service('current_route_match');

Useful Methods

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

Get the route name

$routeName = $service->getRouteName()

  • Returns the route name
  • e.g.: user.canonical, system.404, system.site_maintenance_mode

Get route parameters (including referencing path objects)

$paramObject = $service->getParameter('route_param')

  • Returns the processed value of a named route parameter
  • Example:
    For path /user/{uid}
    $user = $service->getParameter('user') returns user object (Drupal\user\Entity\User) for {uid} value
  • Checkout the routing.yaml path entry to see if parameters are available