79

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?

1

11 Answers 11

147

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.

6
  • why you mention that it would be better to use routes instead of paths ? Can you provide a brief example/reason ? 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). Sep 15 '15 at 20:12
  • 5
    getAliasByPath does return the path without language prefix. Any idea how to solve this? Apr 14 '16 at 14:11
  • 2
    what about from within a twig template?
    – E.A.T
    Nov 24 '16 at 13:10
38

The proper way for the URL without the URL alias.

$current_path = \Drupal::service('path.current')->getPath()
4
  • 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? 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
28

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

2
  • 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
    Mar 6 '18 at 4:17
  • +1 Works in Drupal 9 (9.0.7) I have found. Thank you! Nov 11 '20 at 17:24
18

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

3
  • current_path() and drupal_get_path_alias() they are different functions. Jun 26 '15 at 12:45
  • This should be the answer as this version gives the path with the language prefix. Aug 15 '17 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
    Aug 29 '17 at 13:17
11

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

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

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

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

4

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

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

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

0

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

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

$current_url will look something like this:

http://example.com/my-current-page.html?query_var1=1&query_var2=2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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