Jump to content

API method for complete current URL?


Robin S
 Share

Recommended Posts

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?

  • Like 1
Link to comment
Share on other sites

@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;
}
  • Like 4
Link to comment
Share on other sites

@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

  • 1 year later...
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
  • Like 4
Link to comment
Share on other sites

You mean this one?

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 by szabesz
It does work indeed (PW 3.0.62)
  • Like 1
Link to comment
Share on other sites

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 :P Thanks for your help @thetuningspoon and @Robin S!

Link to comment
Share on other sites

  • 8 months later...
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. 

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...