Trying to get the current page URL

Permalink 3 users found helpful
So its befuddling me to get the current URL of the page a user is on and create a link from it...

How can I retrieve this page attribute?

webjedi
 
Mnkras replied on at Permalink Reply
Mnkras
getCollectionID ()
jgarcia replied on at Permalink Reply
jgarcia
What is the context? Are you wanting to do this within a content block on a page? Are you creating a custom block? Single page?
webjedi replied on at Permalink Reply
webjedi
Well, yes I am going to grab it in the bottom nav block of the page. Essentially getting the current page link to populate in a form.
ScottC replied on at Permalink Reply
ScottC
do:

function getCurrentUrl(){
   global $c;
   $nh = Loader::helper('navigation');
   $cpl = $nh->getCollectionURL($c);
   return $cpl;
}


this is from a controller hence the global $c which is hacky, but it does work.

so say on_start(){
$this->set('currentUrl'=>$this->getCurrentUrl();
}

then in your page/block do your href and that jazz then:

<?php echo $currentUrl ; ?>
tilm4nn replied on at Permalink Reply
tilm4nn
Hey thank you, that helped me a lot :)

Only problem I still have is when I am using this for the top level home page (I use URL rewriting on my page to have pretty URLs).

When I use this script on the top level home page it returns my base url with /index.php?cID=1 appended. On all sub pages however it returns the pretty URL.

Any idea how to influence this to have only the base url returned for the top page? Or is there any way how i can discover that $c is the top level page and can just use BASE_URL instead in this case?

Cheers, Tilmann
ScottC replied on at Permalink Reply
ScottC
no.
thundavolt replied on at Permalink Reply
I did it this way.

<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>


When you have that you can the add:

<?php echo curPageURL();?>
ScottC replied on at Permalink Best Answer Reply
ScottC
I'll revisit. This is how I do it.
function getCurrentUrl(){
$currentPage = Page::getCurrentPage();
Loader::helper('navigation);
return NavigationHelper::getLinkToCollection($currentPage, true);
}


This will append the base url, has a feel good method to wrap around a global, and I think it is the right way to go.
andrew replied on at Permalink Reply
andrew
This is probably the best way to go about it.
triplebox replied on at Permalink Reply
triplebox
Just curious - where/how could one place universal, custom functions like this so that it's stored only once, but could be called from anywhere on the site?
ScottC replied on at Permalink Reply
ScottC
this depends. You can either have a class that extends controller that adds this functionality, then in each "area above the class" for lack of a better term you could do:

Loader::library('awesome_controller');

class DashboardController extends AwesomeController{
//getCurrentUrl is inherited from AwesomeController
}

so in your view $this->controller->getCurrentUrl() would return the current url the controller is mapped to(and in this case the view being rendered).

You could also simply define a function after the dispatcher runs (say in a package or similar) where you provide a function getCurrentRenderedViewURL(){

}

this would be site specific obviously and might be tied to a package install etc, but you have to find somewhere in the dispatcher workflow/execution path where you can introduce the function to call from down the foodchain...for lack of a better word.

That help?
ideasponge replied on at Permalink Reply
ideasponge
How would this work on a site with multiple domain management addons like "Domain Mapper" or "Multiple Domains"?
ScottC replied on at Permalink Reply
ScottC
This actually resolves based off of the parsed URL after the domain. It isn't actually domain aware so I don't know if that is a good or a bad thing in your case depending on if you have many virtual hosts pointing at one install or not.

Scott Conrad
Web Application Engineer
website
CWSpear replied on at Permalink Reply
CWSpear
You can just use the getCollectionPath method. It would probably be the easiest.

So this page's url ishttp://www.concrete5.org/community/forums/customizing_c5/trying-to-...

The following would result in /community/forums/customizing_c5/trying-to-get-the-current-page-url/ if used on this page.
$c->getCollectionPath();
Vahan replied on at Permalink Reply
Vahan
or in a block controller if you want entire url as well (not just the path):
public function on_start() {
   global $c;
   $this->set('currentPath',$c->getCollectionPath());
   // or
   $this->set('currentUrl',Loader::helper('navigation')->getCollectionPath($c));
}

then you can use it anywhere in your block, even in edit.php like this:
<?php echo $currentUrl; ?>


Thanks to ScottC for helping me with this above :)
jeevanism replied on at Permalink Reply
jeevanism
the best way to do this.. thank you a lot