开发者

CakePHP: get current controller name in a model

I'm creating a behaviour that needs to log the current controller name. How can I get the current controller na开发者_开发知识库me from within a model in CakePHP?


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.

Inflector::pluralize($this->name);


$GLOBALS['Dispatcher']->params['controller']

will give you the controller name


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 ?


For anybody revisiting this question and using CakePHP 3.x:

$this->request->getParam('controller')


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜