Drupal 8 current path, route and URI
Create the path as Drupal knows it
$current_url = Url::fromRoute('<current>'); $path = $current_url->toString();
Path and internal path can be different, eg /user/login
// /en/user/login $current_url->toString(); // user/login $current_url->getInternalPath(); // <current> $route_name = $current_url->getRouteName();
The Drupal syntax of the path
$path = \Drupal::request()->attributes->get('_system_path');
The path including query string
$current_uri = \Drupal::request()->getRequestUri();
For the current raw path (the un-aliased Drupal path):
$current_path = \Drupal::service('path.current')->getPath(); $result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
Get the url of the request for displayed on the browser.
$page = \Drupal::request()->getRequestUri();