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(=> [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(
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.