Drupal 10: Get The Current Path And Route From The Request Stack Service

Use the request_stack service in Drupal to get information about the current request. This can be used to find the current path and route like this.

// Get the current path.
$path = \Drupal::requestStack()->getCurrentRequest()->getPathInfo();
// $path now contains "/some-page-path".

// Get the current route.
$requestRouteName = \Drupal::requestStack()->getCurrentRequest()->attributes->get('_route');
// $requestRouteName now contains "entity.node.canonical".

It is generally better to get the route name as this will catch more paths and will be less likely to change.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
5 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.