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?

View Replies: View Best Answer
Mnkras replied on at Permalink Reply
getCollectionID ()
jgarcia replied on at Permalink Reply
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
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

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(){

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

<?php echo $currentUrl ; ?>
tilm4nn replied on at Permalink Reply
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
thundavolt replied on at Permalink Reply
I did it this way.

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

When you have that you can the add:

<?php echo curPageURL();?>
ScottC replied on at Permalink Best Answer Reply
I'll revisit. This is how I do it.
function getCurrentUrl(){
$currentPage = Page::getCurrentPage();
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
This is probably the best way to go about it.
triplebox replied on at Permalink Reply
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
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:


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
How would this work on a site with multiple domain management addons like "Domain Mapper" or "Multiple Domains"?
ScottC replied on at Permalink Reply
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
CWSpear replied on at Permalink Reply
You can just use the getCollectionPath method. It would probably be the easiest.

So this page's url is

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

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
the best way to do this.. thank you a lot