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?
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();
Without query string:
$current_uri \Drupal::request()->getPathInfo()
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.
::getAliasByPath()
will no longer accept a path string without a leading slash (since beta13 I believe).
Commented
Sep 15, 2015 at 20:12
getAliasByPath
does return the path without language prefix. Any idea how to solve this?
Commented
Apr 14, 2016 at 14:11
The proper way for the url without the URL alias.
\Drupal::request()->attributes->get('_system_path');
For the URL with alias:
\Drupal::request()->getRequestUri();
\Drupal::request()->getRequestUri();
respect Drupal installs in a subfolder?
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
{{ url('<current>') }}
even though not listed under Replacement patterns.
{{ path('<current>') }}
For getting the current path in Drupal 7, you use to use current_path()
, but in Drupal 8 you use $url = Url::fromRoute('<current>');
.
current_path()
and drupal_get_path_alias()
they are different functions.
Commented
Jun 26, 2015 at 12:45
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:
\Drupal\Core\Url::fromRoute('<current>', [], ['absolute' => 'true'])->toString()
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:
http://example.com/my-current-page.html?query_var1=1&query_var2=2