CakePHP: Access Current Controller, Action and Parameters

Photo of Mike Dalisay
Modified Monday, December 26, 2011
by - @ninjazhai
Hi guys, today I'm going to show you how to access or get CakePHP's current page controller, action and parameters. I found this useful when high lighting certain elements of a page for example are tabs, sidebars, etc. that tells the user what part of the system or website they are currently in.

CakePHP: Access Current Controller, Action and Parameters

For instance we will have the following URL:

Based on that URL, our controller is users. To access it we will have:

          $current_controller = $this->params['controller']; 

Our action is edit and we will access it this way:

          $current_action =  $this->action;

Our first parameter is 27, which is the ID of the record to be edited. We can access it by doing:

          $user_id =  $this->params['pass'][0];

In case that we have second parameter, we have to access it this way:

          $second_parameter =  $this->params['pass'][1]; 

For the third parameter:

          $third_parameter =  $this->params['pass'][2];  

and so on...

You can do these codes either in your controller or view files.

The Code of a Ninja Resources

For FREE programming tutorials, click the red button below and subscribe! :)
Thanks for the comments!
"First do it, then do it right, then do it better."
~ Addy Osmani
"Talk is cheap. Show me the code."
~ Linus Torvalds
Let's Stay Connected!
g+ r
Android app on Google Play
© 2011-2014 The Code Of A Ninja. All rights reserved. Proudly Powered by Google Blogger. Images, logos, marks or names mentioned herein are the property of their respective owners.