yet another note for myself

CakePHP Tricks for manipulating URL in the view

Your Ad 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.


No related posts.

This entry was posted in CakePHP. Bookmark the permalink.

10 Responses to CakePHP Tricks for manipulating URL in the view

  1. Is it only work with 1.2x version of cakephp?

    I have this error on 1.1x version

    Call to undefined method Router::url()

  2. ragrawal says:

    hi Dedy,

    yes, Router::url is available only in 1.2. I am not sure what is an equivalent method for 1.1 but the API documentation might help you

  3. rafaelbandeira3 says:

    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.

  4. Steve says:

    I use $html->url(‘/’, true); It’s a bit cleaner and it simply references Router::url

  5. techblog says:

    thanks nice work man…

  6. prabha says:

    Can you give some sample code for callback methods and passing parameter to the same?


  7. Learning Richard says:

    nice, thanks

  8. Guillermo says:

    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 “”;


  9. Pingback: CakePHP Tricks for manipulating URL in the view « memento