我正在尝试在Yii2控制器中执行一些代码,因为我需要在行为部分中可以访问模型中的一些代码,因此我可以将模型作为参数传递并避免运行重复查询;但是我还需要能够找出正在调用的动作,但我没有太多运气.
我已经尝试过使用beforeAction但是它似乎在行为代码运行后运行,所以这对我没有帮助.
然后我尝试使用init,但似乎此操作无法通过$this-> action-> id获得.
一些示例代码:
class MyController extends Controller { public $defaultAction = 'view'; public function init() { // $this->action not available in here } public function beforeAction() { // This is of no use as this runs *after* the 'behaviors' method } public function behaviors() { return [ 'access' => [ 'class' => NewAccessControl::className(),'only' => ['view','example1','example2'],'rules' => [ [ 'allow' => false,'authManager' => [ 'model' => $this->model,'other_param' => $foo,'other_param' => $bar,],'actions' => ['view'],// everything else is denied ],]; } public function viewAction() { // This is how it is currently instantiated,but we want to instantiate *before* the behavior code is run so we don't need to instantiate it twice // but to be able to do that we need to know the action so we can pass in the correct scenario $model = new exampleModel(['scenario' => 'view']); } }
authManager只是对AccessRule类扩展中的成员变量的引用.
无论如何我能做到吗?
好吧,如果我找对你,你正在寻找这样的东西:
原文链接:https://www.f2er.com/php/240166.htmlpublic function behaviors() { $model = MyModel::find()->someQuery(); $action = Yii::$app->controller->action->id; return [ 'someBehavior' => [ 'class' => 'behavior/namespace/class','callback' => function() use ($model,$action) { //some logic here } ] ]; }
因为behavior()只是一个方法,所以你可以声明任何变量并添加你想要的任何逻辑,你必须遵循的唯一约定 – 返回类型必须是数组.
如果使用自定义行为,则可以使用events()方法将行为的方法绑定到某些事件.例如.
class MyBehavior extends Behavior { public function events() { return [ \yii\web\User::EVENT_AFTER_LOGIN => 'myAfterLoginEvent',]; } public function myAfterLoginEvent($event) { //dealing with event } }
在此示例中,myAfterLoginEvent将在用户成功登录到应用程序后执行. $event变量将由框架传递,根据事件类型,它将包含不同的数据.阅读about event object
更新:
正如我现在所看到的,我的答案对于事件和行为更为通用.现在,当您添加代码时,我建议您使用以下代码覆盖行为的beforeAction($action)方法:
public function beforeAction($action) { $actionID = $action->id; /* @var $rule AccessRule */ foreach ($this->rules as &$rule) { $model = &$rule->authManager['model']; //now set model scenario maybe like this $model->scenario = $actionID; } //now call parent implementation parent::beforeAction($action); }
另请参阅AccessControl implementation of beforeAction
方法,它调用每个规则允许方法将当前操作作为参数传递给它.因此,如果您有扩展AccessRule的类,则可以覆盖allow($action,$user,$request)方法或matchCustom($action)方法来设置适当的模型方案.希望这会有所帮助.
还有一个选择:
覆盖控制器的runAction($id,$params = [])方法.这里$id是actionID – 正是你需要的.检查id,设置适当的模型场景并调用parent :: runAction($id,$params);