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?


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 ? – darol100 Jun 26 '15 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 Jun 29 '15 at 6:04
  • Just a note that ::getAliasByPath() will no longer accept a path string without a leading slash (since beta13 I believe). – othermachines Sep 15 '15 at 20:12
  • 5
    getAliasByPath does return the path without language prefix. Any idea how to solve this? – Philipp Michael Apr 14 '16 at 14:11
  • what about from within a twig template? – E.A.T Nov 24 '16 at 13:10

The proper way for the URL without the URL alias.

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

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. – darol100 Jun 26 '15 at 12:45
  • This should be the answer as this version gives the path with the language prefix. – Guillaume Bois Aug 15 '17 at 17:49
  • 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 Aug 29 '17 at 13:17

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

  • 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 Mar 6 '18 at 4: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();

For Drupal 8, this is the cleanest solution I was able to find:

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

In Drupal 8 there is function current_path() that return the current URL path of the page being viewed.


http://example.com/node/306 returns "node/306".
http://example.com/drupalfolder/node/306 returns "node/306" while base_path() returns "/drupalfolder/".
http://example.com/path/alias (which is a path alias for node/306) returns "node/306" as opposed to the path alias.

see here https://api.drupal.org/api/drupal/core%21includes%21common.inc/function/base_path/8.0.x


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();

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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