How can I get the current alias and/or path in Drupal 8?

In Drupal 7 I did this with drupal_get_path_alias(). What is the equivalent code I should use in Drupal 8?


11 Answers 11


Some of the other answers were only correct during previous alpha/beta versions of Drupal 8, or seemed to be incomplete. As of beta7 (and hopefully permanently), the following rules should apply:

For the current raw path (the un-aliased Drupal path):

$current_path = \Drupal::service('path.current')->getPath();

For the current URI, which is pretty-much a direct representation of the request (it may even include a query string):

$current_uri = \Drupal::request()->getRequestUri();

There is of course no guarantee that this $current_uri value will give you an alias, even if one is available for the request, as it only represents what the user has requested. So to strictly do what you are asking about (get alias if available, and path if not) I think you could do:

$current_path = \Drupal::service('path.current')->getPath();
$result = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);

Of course, depending on the type of processing you are doing it may be best to be working with routes instead of paths, but I suppose that's a whole other topic.

  • why you mention that it would be better to use routes instead of paths ? Can you provide a brief example/reason ? Commented Jun 26, 2015 at 12:48
  • @darol100, I may not be the best person to provide a comprehensive answer for that, but I can tell you that the use of paths for programmatic URL generation is officially depreciated in Drupal 8 - see: drupal.org/node/2046643. Symfony routes provide a more robust way to map resources independent of the path structures (if you build a link using routes, that link will keep working even if the URL path/pattern changes).
    – rjacobs
    Commented Jun 29, 2015 at 6:04
  • Just a note that ::getAliasByPath() will no longer accept a path string without a leading slash (since beta13 I believe). Commented Sep 15, 2015 at 20:12
  • 6
    getAliasByPath does return the path without language prefix. Any idea how to solve this? Commented Apr 14, 2016 at 14:11
  • I agree about the route name subject. For example, if you want to check if you're in the /user/xxx url, it would be better to use : drupal.stackexchange.com/questions/202831/… > if (\Drupal::routeMatch()->getRouteName() === 'entity.user.canonical') than checking the path.
    – Pauleau
    Commented Jun 16, 2021 at 9:46

The proper way for the url without the URL alias.


For the URL with alias:

  • Does \Drupal::request()->getRequestUri(); respect Drupal installs in a subfolder?
    – Brian
    Commented Jul 4, 2014 at 1:26
  • how would i do that for a not current path? is there a way to get it from just a string? Commented Apr 6, 2016 at 19:25
  • How do I get the absolute path, such as this: mydrupalsite.org/node/123 ?
    – mcaleaa
    Commented Aug 10, 2016 at 16:01
  • 1
    global $base_root; $current_uri = \Drupal::request()->getRequestUri(); $current_path = $base_root . $current_uri;
    – oknate
    Commented Dec 20, 2016 at 17:21

In Drupal 8 you can do this with Twig:

{{ url('<current>') }}


<a href="{{ url('<current>') }}">{{ 'Reload'|t }}</a>

from: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates

  • 1
    Great find @nicola-de-lazzari - Also Your answers works in Views as well. View > Fields > Rewrite Results > Link path accepts {{ url('<current>') }} even though not listed under Replacement patterns.
    – piet
    Commented Mar 6, 2018 at 4:17
  • +1 Works in Drupal 9 (9.0.7) I have found. Thank you! Commented Nov 11, 2020 at 17:24
  • 3
    Note : to have the path instead of the full URL you can use {{ path('<current>') }}
    – Erwan
    Commented Mar 7, 2022 at 15:36

For getting the current path in Drupal 7, you use to use current_path(), but in Drupal 8 you use $url = Url::fromRoute('<current>');.

Source: https://www.drupal.org/node/2382211

  • current_path() and drupal_get_path_alias() they are different functions. Commented Jun 26, 2015 at 12:45
  • 1
    This should be the answer as this version gives the path with the language prefix. Commented Aug 15, 2017 at 17:49
  • 1
    The OP is asking for the D8 equivalent of drupal_get_path_alias() . You are returning the current path and NOT the aliased path from the D7 'node/'.$nid
    – macjules
    Commented Aug 29, 2017 at 13:17

To get current path (aliased path):

// Returns something like /about
$url = \Drupal\Core\Url::fromRoute('<current>');

To get current path (system path):

// Returns node/1
$path = $url->getInternalPath();

To get full current url not just the path (Drupal 8), this is the cleanest solution I was able to find:

  • 7
    \Drupal\Core\Url::fromRoute('<current>', [], ['absolute' => 'true'])->toString()
    – leymannx
    Commented Dec 2, 2016 at 13:09

In Twig template / Drupal 8 :

URL of the current page

{{ url('<current>') }}

only PATH

{{ path('<current>') }}

See: https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates


If you want the absolute URL with the querystring too:

use Drupal\Core\Url;
// ...
Url::fromRoute('<current>', [], ['query' => \Drupal::request()->query->all(), 'absolute' => 'true'])->toString();

Get the current route name:

$current_route = \Drupal::routeMatch()->getRouteName();

Get current query parameters (upscaled):

$current_parameters = \Drupal::routeMatch()->getParameters();

Get current URL as an object of type Drupal\Core\Url, which requires the query parameters to be an array of values rather than upscaled objects

use Drupal\Core\Url;

$url = Url::fromRoute(\Drupal::routeMatch()->getRouteName(), \Drupal::routeMatch()->getRawParameters()->all());

Since Drupal 8.8.0, the path.alias_manager has been deprecated in favor of the new service path_alias.manager (see the change record.)

Accordingly, you can get the current path alias this way:

$current_path = \Drupal::service('path.current')->getPath(); $alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);


To get the full current Url it is best to use this:

$current_url = \Drupal::request()->getUri();

$current_url will look something like this:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.