CakePHP中的$ this->动作变量是什么?

时间:2013-12-17 13:16:08

标签: php arrays cakephp cakephp-2.0

大家好我在Simple Authentication and Authorization Application Section的Cakephp教程中包含的这行代码中需要帮助:

if (in_array($this->action, array('edit', 'delete'))){

}

我知道the in_array()函数的功能但我不知道$this->action的全部含义及其包含的内容,$this->action首先做了什么?

任何人都可以帮助我吗?

5 个答案:

答案 0 :(得分:3)

$ this->操作会返回当前操作的名称 例如 : 您的请求= /发布/编辑 在控制器中:

class PostsController extends AppController{
   // ...
   public function beforeFilter(){
       parent::beforeFilter();
       debug($this->action); // = edit
   }
// ...
}

答案 1 :(得分:1)

您可以使用parent::beforeFilter();语句进行用户验证。

在你的控制器中

你必须使用

function beforefilter()
{
     parent::beforeFilter();
}

此功能将检查用户权限

答案 2 :(得分:0)

$ this-> params ['action'] 而不是 $ this-> action

答案 3 :(得分:0)

$this->action returns the action name of controller

在您的情况下,in_array($ this-> action,array('edit','delete')检查操作名称是编辑还是删除。

for debugging you can use 

pr($this->action);

答案 4 :(得分:0)

或者更好的尝试

debug($this)

这也会让你知道其他类变量。