Get current URL in CakePHP

As a developer we might need to get current url in cakephp to show it or use it on view page or to store it to some variable for reference,  we can use CakePHP internal function to retrieve current URL or relative path. I always wanted to use some inbuilt solution rather than using traditional PHP code i.e. $_SERVER, after several minutes of efforts, I found this code.

So if you want to get current url in cakephp, than try below code :get current url in cakephp

echo Router::url($this->here, true);

This will give us full URL with hostname.

If you want to get relative path instead of full URL, use the following code:

echo $this->here;

This will produce absolute URL excluding hostname i.e. /controller/abc/xyz/

For more information, visit http://www.cakedc.com/

Do comment if you have any question!

 “I am PHP Freelancer having more than 7 years of experience, if you want to do any project with me then you can contact me.”

Be Social, Share!