8

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!

14

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.

  • 1
    In 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. – Simon East May 21 '18 at 2:13
20

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!

  • Any way to exclude query strings? – Laurence Cope Nov 1 at 13:51
1

In Craft 3 and PHP you can use \Craft::$app->getRequest()->absoluteUrl or \Craft::$app->request->absoluteUrl

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.