0

Yii 中是否可以调用一个事件处理程序,以便它在每个控制器操作调用上执行。基本上我有一个 RESTful 应用程序。目前,对于每个请求,它都会显式调用身份验证函数。我想要的是在发出任何请求时调用身份验证函数。

我做了什么

class MyController extends RestController{
 public function actionDosomething(){
  $this->authenticate();// I don't want this line to be put in every controller action.
 }
}
4

2 回答 2

1

另一种选择(在我看来更像 Yii 的方法)是编写一个过滤器,然后使用该filters方法根据需要应用它。

将来它将为您提供更大的灵活性: http ://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter

于 2014-01-07T14:50:21.120 回答
1

你的答案是beforeAction回调。把它放在你的主控制器文件中。

public function beforeAction($action) {

     if(in_array($action, array( /* you list of actions */ ))) 
     {
       //do your thing
     }
}
于 2014-01-07T13:57:05.273 回答