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.

Comments

Thanks for this!

I am looking for a good way describe what happens to requests to Drupal:

  1. to end users what happens when they actually use a websites.

     2.  to our infrastructure people...and boy do they like detail.

 

 

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
3 + 1 =
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.