CakePHP:获取模型中的当前控制器名称
我正在创建一个需要记录当前控制器名称的行为。如何从 CakePHP 的模型中获取当前控制器名称?
I'm creating a behaviour that needs to log the current controller name. How can I get the current controller name from within a model in CakePHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我知道这个问题已经很老了,但正确的解决方案是:
$this->params['controller']
有关“params”属性的更多信息:
http://book.cakephp.org/view/963/The-Parameters -属性参数
I know this question is pretty old, but the proper solution here would be:
$this->params['controller']
For more information on the 'params' attribute:
http://book.cakephp.org/view/963/The-Parameters-Attribute-params
试试这个。如果您只需要控制器的名称。
Try this. if u only need the name of the controller.
会给你控制器名称
will give you the controller name
我找到了一个解决方案,它并不漂亮,但对我有用。我只是使用 $_REQUEST['url'] 并通过 url 捕获控制器名称。该解决方案的缺点是,如果您的默认路线不同,则该解决方案将不起作用...有人有更好的方法吗?
I found a solution, it's not pretty but worked for me. I just use the $_REQUEST['url'] and catch de controller name by url. The downside of this solution, is that if you have a route different for default, this solution will not work... anyone have a better approach ?
对于任何重新审视这个问题并使用 CakePHP 3.x 的人:
For anybody revisiting this question and using CakePHP 3.x:
PHP 的魔法常量之一是
__CLASS__
,它将返回其所在对象的类名。这可能会满足您的需要。http://php.net/manual/en/language.constants.predefine.php
魔法常量和方法很有趣。
One of PHP's Magic Constants is
__CLASS__
which will return the class name of the object it is within. This may get you what you need.http://php.net/manual/en/language.constants.predefined.php
Magic Constants and Methods are fun.