Robin S Posted April 24, 2016 Share Posted April 24, 2016 Is there an API method that gives the complete URL of the currently viewed page as it is known to PW? So not including any hashes or other URL changes that might have been made by Javascript (PW can't know about those) but basically $page->url + any url segments + any page numbers. Or does this have to be built manually from the other $input methods? 1 Link to comment Share on other sites More sharing options...
adrian Posted April 24, 2016 Share Posted April 24, 2016 $page->httpUrl Link to comment Share on other sites More sharing options...
Robin S Posted April 24, 2016 Author Share Posted April 24, 2016 @adrian Thanks, but $page->httpUrl doesn't include url segments or page numbers. Link to comment Share on other sites More sharing options...
netcarver Posted April 24, 2016 Share Posted April 24, 2016 @Robin, You might be able to get most of what you need from the $_SERVER['REQUEST_URI'] value. You may have to prepend the scheme and host. 1 Link to comment Share on other sites More sharing options...
Robin S Posted April 24, 2016 Author Share Posted April 24, 2016 @netcarver That's perfect, thanks. Getting the full URL using API methods only is surprisingly long-winded. This is the best I could manage: $full_url = $page->url; if($input->urlSegmentsStr) { $full_url .= $input->urlSegmentsStr . "/"; } if($input->pageNum > 1) { $full_url .= $config->pageNumUrlPrefix . $input->pageNum; } 4 Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 24, 2016 Share Posted April 24, 2016 If you just need the url for a redirect this does work: $session->redirect('.'); 6 Link to comment Share on other sites More sharing options...
adrian Posted April 24, 2016 Share Posted April 24, 2016 @adrian Thanks, but $page->httpUrl doesn't include url segments or page numbers. Yeah sorry about that - I read your request in a hurry and saw "So not including any hashes" and extrapolated the "not" to the url segments etc. Link to comment Share on other sites More sharing options...
szabesz Posted July 25, 2017 Share Posted July 25, 2017 On 2016-4-24 at 11:24 AM, Robin S said: Getting the full URL using API methods only is surprisingly long-winded. This is the best I could manage: I have turned it into a Page method: /* Returns the complete URL of a Page. * * @param int arguments[0] Pass false to exclude the pageNum of the Paginator * @return string */ $wire->addHookMethod('Page::siteCompleteUrl', function($event) { $withPageNum = isset($event->arguments[0]) ? $event->arguments[0] : true; $full_url = page()->url; if (input()->urlSegmentsStr) { $full_url .= input()->urlSegmentsStr; } if ($withPageNum && input()->pageNum > 1) { $full_url .= "/" . config()->pageNumUrlPrefix . input()->pageNum; } $event->return = $full_url; }); Small changes were applied to it: "/" is added in the second "if" to avoid double slashes on the pages of the paginator first argument can be used to exclude the page number of the paginator, defaults to true 4 Link to comment Share on other sites More sharing options...
Robin S Posted July 25, 2017 Author Share Posted July 25, 2017 @szabesz, I think this is covered by $input->url() and $input->httpUrl() now, which were introduced since I wrote my post above. There's a blog post about it somewhere but I can't find it at the moment. 3 Link to comment Share on other sites More sharing options...
szabesz Posted July 25, 2017 Share Posted July 25, 2017 (edited) You mean this one? https://github.com/processwire/processwire/blob/master/wire/core/WireInput.php#L504 https://processwire.com/api/ref/input/http-url/ Does not seem to work. I get no query string. Not to mention the lack of urlSegmentsStr support, at least I cannot see it or looking at a completely wrong place... I dunno At least your snippet works fine. Thanks once more! Edited July 26, 2017 by szabesz It does work indeed (PW 3.0.62) 1 Link to comment Share on other sites More sharing options...
thetuningspoon Posted July 26, 2017 Share Posted July 26, 2017 Looks like you need to use $input->url(true) to get the query string with it. But maybe you already tried that? 2 Link to comment Share on other sites More sharing options...
szabesz Posted July 26, 2017 Share Posted July 26, 2017 3 hours ago, thetuningspoon said: Looks like you need to use $input->url(true) to get the query string with it. But maybe you already tried that? I should have gone to bed last night instead of... It does work indeed (PW 3.0.62) I was probably on the wrong page of the site to test it but around midnight it is sometimes hard to tell things apart Thanks for your help @thetuningspoon and @Robin S! Link to comment Share on other sites More sharing options...
thetuningspoon Posted July 26, 2017 Share Posted July 26, 2017 Looks like we need to add this to the cheatsheet. The need for this came up for me a while back and I didn't know about this function, and just yesterday one of my coworkers asked me if there was a function for this! 3 Link to comment Share on other sites More sharing options...
adrian Posted March 26, 2018 Share Posted March 26, 2018 Just noticed that using: $input->url(true) to get urlsegments doesn't work, you need: $input->httpUrl(true) Does this sound like a bug to you guys? According to the docs (https://processwire.com/api/ref/input/url/) it should work. Link to comment Share on other sites More sharing options...
adrian Posted March 26, 2018 Share Posted March 26, 2018 Actually, I got that wrong - the problem is that you need the trailing slash after the urlsegment for it to work, which doesn't seem right to me since it can be optional. 1 Link to comment Share on other sites More sharing options...
szabesz Posted March 27, 2018 Share Posted March 27, 2018 12 hours ago, adrian said: the problem is that you need the trailing slash after the urlsegment for it to work Maybe this is the issue I tripped over? I just did not bother looking into it as I did not have time to do so. I had reverted back to the hook I posted above and using it ever since. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now