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?
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?
$this->name
also give you controller's name. Their difference with$this->params['controller']
is it's first letter capitalizedResults in:
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 fileNote: don't forget to declare it into the controller you'll use for example:
$this->Admin->_getController('MyControllerName')
var $helpers = array('Html', 'Form', 'Admin');
function _getController(...
You can get controller like this:
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 filesOther data about request can be taken like this:
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.
To get the current controller, Try this :
$this->params['controller']
To get the current action, Try this :
$this->params['action']
.Use
$this->params['controller']
to get the current controller.You can do a
debug($this->params)
to see other available variables.I am using cakephp 3.2
Following code is working properly in cakephp 3.2