Very simple request!
All I want to do is to be able to get the full URL of the page & output that in the HTML (for a 'share this page' link).
Can anyone help please?
Many thanks!
3 Answers
Craft 3
In Craft 3 this is now {{ craft.app.request.absoluteUrl }}
— for those who land here from a search and want to avoid using the deprecation notice to update it! (see answer from @natebeaty)
And in case you are looking for more options like {{ craft.app.request.pathInfo }}
to get current url path, look up the Craft 3 Craft Web Request Class
Craft 2
{{ craft.request.getUrl() }}
or
{{ craft.request.url }}
If you need to include the query string (like ?foo=bar
), this should do it:
{{ craft.request.getHostInfo() ~ craft.request.getRequestUri() }}
Beyond that, craft.request
is mapped to the HttpRequestVariable class, which contain a plethora of methods and properties to render information about the current request.
-
1In Craft 2 you can use
{{ url('/page') }}
in Twig or\Craft\UrlHelper::getUrl('/page')
in PHP to get an absolute URL for a given path. May 21, 2018 at 2:13
In Craft 3 this is now {{ craft.app.request.absoluteUrl }}
— for those who land here from a search and want to avoid using the deprecation notice to update it!
-
2
-
Not sure if there's a more elegant way (I couldn't find anything digging around, closest was
craft.app.request.fullPath
), but this does the trick:craft.app.request.absoluteUrl|replace('?'~craft.app.request.queryStringWithoutPath,'')
Jan 27, 2020 at 1:45 -
1Try using the built in URL helper, like so
{{ url(craft.app.request.fullPath) }}
to remove the query string Oct 2, 2020 at 15:06
In Craft 3 and PHP you can use \Craft::$app->getRequest()->absoluteUrl
or \Craft::$app->request->absoluteUrl