【发布时间】:2012-10-23 15:43:32
【问题描述】:
在 View 中,我可以使用
$this->action
但是,我无法通过
获取控制器名称$this->controller
在 View 中获取当前控制器的正确方法是什么?
【问题讨论】:
-
如果您需要它来构建指向 current 控制器中的操作的链接,请记住您可以在路由数组中省略
'controller'键.
标签: cakephp-2.0
在 View 中,我可以使用
$this->action
但是,我无法通过
获取控制器名称$this->controller
在 View 中获取当前控制器的正确方法是什么?
【问题讨论】:
'controller' 键.
标签: cakephp-2.0
使用$this->params['controller'] 获取当前控制器。
您可以通过debug($this->params) 查看其他可用变量。
【讨论】:
$this-params的提示!
$this->params['controller'] 返回:something_foo。这意味着您正在使用somethingFooController
你可以像这样得到控制器:
echo "<pre>controller:".$this->request->params['controller']."</pre>";
虽然$this->params 更短,但$this->request->params 对自动完成更友好。您可以从这个问题中检查自动完成选项:PHPStorm autocomplete for CakePHP custom helpers in view files
关于请求的其他数据可以这样获取:
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>";
编辑:
从 CakePHP 3 开始,$this->params 快捷方式被删除。所以对于 CakePHP 3,你应该使用 $this->request->params['controller']。
http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#id2
另请注意,控制器的第一个字符是大写的。在 Cakephp 2 中是小写的。
【讨论】:
#code_here#";你可能想使用“debug()”
$this->name 也给你控制器的名字。
他们与$this->params['controller'] 的区别在于首字母大写
debug($this->name);
debug($this->params['controller']);
结果:
\app\Controller\AppController.php (line 176)
'Users'
\app\Controller\AppController.php (line 177)
'users'
【讨论】:
要获取当前控制器,试试这个:$this->params['controller']
要获取当前操作,试试这个:$this->params['action']。
【讨论】:
我正在使用 cakephp 3.2
$this->params['controller'] - It is not working, showing error message as bellow..
“缺少助手”
以下代码在 cakephp 3.2 中正常工作
$this->request->params['controller'] - Working
【讨论】:
对于 cakephp 3.6 及更高版本:
虽然上述解决方案可以工作,但它会给出不推荐使用的警告,并且在 cakephp 4 中不起作用。 所以最好使用下面的代码来获取控制器名称。它也适用于视图页面和控制器。
$this->request->getParam('controller')
【讨论】:
要获取电流,
$this->params['controller']
$this->params['action']
$this->params['pass']
【讨论】:
所有其他解决方案都是获取控制器名称...我需要控制器本身,所以我在 view.ctp 文件中由$this->Admin->_getController('MyControllerName') 调用的 AdminHelper.php 中执行了以下函数
/******************************************************************
*
******************************************************************/
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;
}
注意:不要忘记将它声明到您将使用的控制器中,例如:
$this->Admin->_getController('MyControllerName') var $helpers = array('Html', 'Form', 'Admin');
function _getController(...
【讨论】: