Trying to get the current page URL
Permalink 3 users found helpfulHow can I retrieve this page attribute?

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 ; ?>
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
<?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();?>
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.
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?
Scott Conrad
Web Application Engineer
website
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();
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 :)