CakePHP: Access Current Controller, Action and Parameters

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:

http://somedomain.com/users/edit/27/

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.

Hi! I'm Mike Dalisay, the co-founder of codeofaninja.com, a site that helps you build web applications with PHP and JavaScript. Need support? Comment below or contact [email protected]

I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on LinkedIn, Twitter, Facebook, and Instagram.