Get Controller, Action, URL information from Views/Elements

It is sometime very useful to know the name of the controller/action that is calling the view/elements.

Suppose you want to create a menubar/navigation element, where you want to highlight the menuitem which is now active. Now, it will be easier for you to highlight the menuitem accordingly if you can get the url or controller/action name from the element’s code.

You can get this easily, every view or element gets a parameter by default in cakePHP called $params. The $params is an associative array that looks like-

Array (
[controller] => controller_name
[action] => action_name
[url] => Array ( [url] => cake_url )
)

So, by calling $params['url']['url'] from your views/elements you will get the current url. Thanks.


About this entry