CakePHP Tricks for manipulating URL in the view

Note: This blog is now over here

This week, I learnt many new tricks in CakePHP. Below are some of them

1. Get current URL in the view (Thanks to Grant Cox for this one) :
Use  Router::url(“”, true) to get current URL in the view. The second boolean parameter tells whether the path should be absolute or relative. Ideally Router::url(“”, true) should return an absolute URL of the current view, but it always returns the relative URL. So the hack to get the absolute URL is

$absolute_url  = FULL_BASE_URL + Router::url(“”, false);

if you use Router::url(“/”, true), it will return absolute url to your base web directory and this works as desired :p

2. Getting URL parameters in the view (Thanks to AD for this one):
You can get access to all the parameters in the view using $this->params. This will return you an array in this format

[‘pass’] => Array([0]=> [key1]=>v1 [key2] => v2……),
[‘action’] => ‘action_name’,



3. Passing an Array as second argument to $html->link (Again thanks to AD):

You can pass an array of parameters as the second parameter to $html->link. For instance, If I have an array that looks like this

$parameters = Array(
[‘controller’] =>’controller_name’,
[‘action’] =>’action_name’,


To build a url from the above array, you can use $html->link($title, $parameters); This will return a string, “/controller_name/action_name/key1:v1/key2:v2/

I think to notice is the difference in array format that you get using $this->params and the one that you provide to $html->link. $this->params returns an array with a key “pass”, that contains all the key=>value pairs. Whereas, to build a url from an array, you need to specify all the key=>value at the same level at which you specify controller and action.


9 thoughts on “CakePHP Tricks for manipulating URL in the view

  1. You can simply use $this->here in views to get the current URL, it is easier and tells a lot more about your code, Router::url(”, true) says nothing. Even to get the full url I think one should use Router::url($this->here, true). Why do you say the second param doesn’t work?

    It would be greate to say that every url param in cake can be passed as an array ( Controller::redirect, AuthComponent::$loginAction… ) as they all use Router::url to parse arrays.

    Your code on 3. has superfluos brackets in the array definition wich would lead to parse error.

  2. Nice info. I used this to create a dynamic menu.

    $menu_links = array(
    “: index :” => “/users_words/index”,
    “editorials” => “/editorials/index”,
    “map” => “/maps/index”,
    “: cloud :” => “/users_words/display”

    if ($session->check(‘’))
    $menu_links[“: logout :”] = “/users/logout”;
    $menu_links[“: login :”] = “/users/login”;

    foreach ($menu_links as $item=>$url):

    $flag = (“/”.$this->params[‘controller’].”/”.$this->params[‘action’] == $url) ? array(‘id’ => ‘current’) : “”;

    echo “”;
    echo $html->link($item,$url,$flag);
    echo “”;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s