How do I get the current path alias or path?

Solutions

The proper way for the URL without the URL alias.

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

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.

In Drupal 8 you can do this with Twig:

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

example:

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

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

Similar questions

Use taxonomy path for a node's path alias
Is it possible to use a token to reference the alias of a taxonomy term in the pattern of an automatic alias for a node type where that term has been selected, in Drupal 8? (Let's assume a single taxonomy term is selected.) For example: If one has a taxonomy of Names where the values are James, Margaret, Daniel and each name has a path assigned to ...
Why the path alias is like http://domain/http://domain/path?
Adding a simple alias for media [media:url] causes the domain to be listed twice in the URL on media listing page in admin, for example http://domain/http://domain/path. How do I change it to http://domain/path?
How do I translate view's page path alias that has a dynamic path?
I have a view that has a dynamic path and I want to create a path alias for each language installed on my site. Following the answer on How do I translate view's page path alias? I'm able to create a path alias when no dynamic path is involved in the view, but when a contextual filter with dynamic path is in the view I can't find how to create the ...
How do I remove the default node path when using a path alias?
When I add path aliases, how do I remove the system path (/node/[nid]) in Drupal 8, to avoid Google to index the page twice?
How to support legacy url alias' and/or support multiple url alias' (Conditional Pathauto?)
I am migrating a site from wordpress and need to preserve my url structure whilst supporting native pathauto urls in the future (for new content) Overall Drupal's clean url process is almost identical to wordpress but I am finding some small discrepancies and with around 2k posts to migrate over I need a programmatic way to do this that won't feel ...
Automatic alias overriding manual alias on each edit
Does anyone have an issue with Automatic alias, where the setting keeps turning back to auto on each edit of a node? What we would like to achieve is a) some node paths should be generated automatically from the title and remain automatically managed b) for other paths we want to override the alias The problem is only with b, where after any edit t...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.