CakePHP: Access Current Controller, Action and Parameters

Last updated on July 26, 2021 by Mike Dalisay

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.

By Mike Dalisay

Hi, I'm Mike Dalisay, a coder and startup founder. Thank you for visiting! I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on Twitter, LinkedIn, Facebook, and Instagram.

Back to top