Skip to main content
linked the service definition
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Note

Note: The accepted response for this question is out of date: see this change record for best practice. The accepted response for this question is out of date: See Request parameters, route name, and route object should be accessed via RouteMatch.

You should use the CurrentRouteMatchcurrent_route_match service to retrieve the current route, and then read or modify its parameters. This wraps any Symfony etc. magic.

In procedural code, you would write:use code similar to the following one.

$route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();
$title = $route->getDefault('_title');

In object-oriented code, you should implementuse dependency injection and inject the service current_route_match (this is what \Drupal::routeMatch() class does internally anyway.)

Note: The accepted response for this question is out of date: see this change record for best practice.

You should use the CurrentRouteMatch service to retrieve the current route, and then read or modify its parameters. This wraps any Symfony etc. magic.

In procedural code, you would write:

$route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();
$title = $route->getDefault('_title');

In object-oriented code, you should implement dependency injection and inject the service current_route_match (this is what \Drupal::routeMatch() class does internally anyway.)

Note

The accepted response for this question is out of date: See Request parameters, route name, and route object should be accessed via RouteMatch.

You should use the current_route_match service to retrieve the current route, and then read or modify its parameters. This wraps any Symfony etc. magic.

In procedural code, you would use code similar to the following one.

$route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();
$title = $route->getDefault('_title');

In object-oriented code, you should use dependency injection and inject the service.

Removed PHP tag
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

Note: The accepted response for this question is out of date: see this change record for best practice.

You should use the CurrentRouteMatch service to retrieve the current route, and then read or modify its parameters. This wraps any Symfony etc. magic.

In procedural code, you would write:

<?php

$route = \Drupal::routeMatch()->getCurrentRouteMatch()
  ->getRouteObject();
$title = $route->getDefault('_title');

In object-oriented code, you should implement dependency injection and inject the service current_route_match (this is what \Drupal::routeMatch() class does internally anyway.)

Note: The accepted response for this question is out of date: see this change record for best practice.

You should use the CurrentRouteMatch service to retrieve the current route, and then read or modify its parameters. This wraps any Symfony etc. magic.

In procedural code, you would write:

<?php

$route = \Drupal::routeMatch()->getCurrentRouteMatch()
  ->getRouteObject();
$title = $route->getDefault('_title');

In object-oriented code, you should implement dependency injection and inject the service current_route_match (this is what \Drupal::routeMatch() class does internally anyway.)

Note: The accepted response for this question is out of date: see this change record for best practice.

You should use the CurrentRouteMatch service to retrieve the current route, and then read or modify its parameters. This wraps any Symfony etc. magic.

In procedural code, you would write:

$route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();
$title = $route->getDefault('_title');

In object-oriented code, you should implement dependency injection and inject the service current_route_match (this is what \Drupal::routeMatch() class does internally anyway.)

Source Link
J-P
  • 485
  • 1
  • 6
  • 8

Note: The accepted response for this question is out of date: see this change record for best practice.

You should use the CurrentRouteMatch service to retrieve the current route, and then read or modify its parameters. This wraps any Symfony etc. magic.

In procedural code, you would write:

<?php

$route = \Drupal::routeMatch()->getCurrentRouteMatch()
  ->getRouteObject();
$title = $route->getDefault('_title');

In object-oriented code, you should implement dependency injection and inject the service current_route_match (this is what \Drupal::routeMatch() class does internally anyway.)