In View(CakePHP), the proper way to get current controller?
Asked Answered
P

9

36

In View, I can get action by using

$this->action

But, I cannot get controller name by

$this->controller

What is the proper way to get current controller in View?

Pushup answered 23/10, 2012 at 15:43 Comment(1)
If you need this to build links to actions in current controller, remember that you can just omit the 'controller' key in the routing array.Theurer
A
81

Use $this->params['controller'] to get the current controller.

You can do a debug($this->params) to see other available variables.

Antananarivo answered 24/10, 2012 at 0:31 Comment(1)
$this->params['controller'] returns: something_foo. This means you are using the somethingFooControllerYount
C
12

You can get controller like this:

echo "<pre>controller:".$this->request->params['controller']."</pre>";

Although $this->params is shorter, $this->request->params is more autocomplete friendly. You can check the autocomplete options from this question: PHPStorm autocomplete for CakePHP custom helpers in view files

Other data about request can be taken like this:

echo "<pre>action:".$this->request->params['action']."</pre>";

echo "<pre>request:"; print_r( $this->request ); echo "</pre>";

echo "<details><summary>this:</summary><pre>"; 
      print_r( $this ); echo "</pre></details>";

Edit:
As of CakePHP 3 $this->params shortcut is removed. So you should user $this->request->params['controller'] for CakePHP 3.
http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#id2
Also note that first character of controller is uppercase. It was lowercase in Cakephp 2.

Col answered 18/3, 2014 at 17:35 Comment(1)
Instead of echo "<pre>#code_here#</pre>"; you might want to use "debug()"Dolly
H
10

$this->name also give you controller's name. Their difference with $this->params['controller'] is it's first letter capitalized

debug($this->name);
debug($this->params['controller']);

Results in:

 \app\Controller\AppController.php (line 176)

'Users'

\app\Controller\AppController.php (line 177)

'users'
Holbert answered 27/9, 2013 at 9:20 Comment(0)
L
6

I am using cakephp 3.2

$this->params['controller'] - It is not working, showing error message as bellow..

"Missing Helper"

Following code is working properly in cakephp 3.2

$this->request->params['controller'] - Working
Ligature answered 19/5, 2016 at 7:23 Comment(0)
B
4

To get the current controller, Try this :$this->params['controller']

To get the current action, Try this :$this->params['action'].

Burgh answered 18/6, 2013 at 13:44 Comment(0)
F
3

For cakephp 3.6 and later:

Although above solutions will work but it gives deprecated warning and will not work in cakephp 4. So It is better to use the following code to get the controller name. It will work in view page and controllers as well.

$this->request->getParam('controller')
Fulguration answered 22/11, 2018 at 12:32 Comment(0)
P
2

To get the current,

  • controller: $this->params['controller']
  • action: $this->params['action']
  • arguments:$this->params['pass']
Persevering answered 10/9, 2013 at 5:43 Comment(0)
I
0

All the other solutions are to get the controller name... I need the controller itself, so I did the following function in an AdminHelper.php called by $this->Admin->_getController('MyControllerName') into the view.ctp file

/******************************************************************
 * 
 ******************************************************************/
function _getController( $pControllerName ){
    if ( ! isset($this->controllersArray[$pControllerName]) ){
        $importRes = App::import('Controller', $pControllerName);// The same as require('controllers/users_controller.php');
        $strToEval = "\$controller = new ".$pControllerName."Controller;";
        $evalRes = eval($strToEval);
        if ( $evalRes === false ){
            throw new AppException("Eval returned an error into ".__FILE__." getController()");
        }
        $controller->constructClasses();// If we want the model associations, components, etc to be loaded
        $this->controllersArray[$pControllerName] = $controller;
    }

    $result = $this->controllersArray[$pControllerName];
    return $result;
}

Note: don't forget to declare it into the controller you'll use for example:

  • people/view.ctp -> $this->Admin->_getController('MyControllerName')
  • PeopleController.ctp -> var $helpers = array('Html', 'Form', 'Admin');
  • AdminHelper.ctp -> function _getController(...
Isaacson answered 31/1, 2016 at 21:41 Comment(0)
O
0

Since: 3.7.7 to get the View's controller name use $this->getName();

Owing answered 12/6, 2023 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.