我正在尝试在Yii2控制器中执行一些代码,因为我需要模型中的一些代码可以在behaviors部分中访问,所以我可以将模型作为参数传递并避免运行重复的查询; 但是我也需要能够找到action所谓的内容,但我没有太多运气.
我尝试过使用beforeAction但似乎在代码运行后会behaviours运行,所以这对我没用.
然后我尝试使用init,但似乎从那时起action就无法使用$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']);
}
}
Run Code Online (Sandbox Code Playgroud)
authManager只是member variable对AccessRule类的扩展内部的引用.
无论如何我能做到吗?
好吧,如果我找对你,你正在寻找这样的东西:
public 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
}
]
];
}
Run Code Online (Sandbox Code Playgroud)
因为behaviors()它只是一个方法,你可以声明任何变量并添加你想要的任何逻辑,你必须遵循的唯一约定 - 返回类型必须是一个数组.
如果您使用自定义行为,则可以使用events()方法将行为的方法绑定到某些事件.例如
class MyBehavior extends Behavior
{
public function events()
{
return [
\yii\web\User::EVENT_AFTER_LOGIN => 'myAfterLoginEvent',
];
}
public function myAfterLoginEvent($event)
{
//dealing with event
}
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,myAfterLoginEvent将在用户成功登录到应用程序后执行.$event变量将由框架传递,并且根据事件类型,它将包含不同的数据.阅读关于事件对象
更新:
正如我现在所看到的,我的答案对于事件和行为更为通用.现在,当您添加代码时,我建议您使用以下代码覆盖行为的 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);
}
Run Code Online (Sandbox Code Playgroud)
另请参阅AccessControl 方法的实现beforeAction,它调用每个规则allows方法,并将当前操作作为参数传递给它.因此,如果您有扩展AccessRule的类,则可以覆盖allow ($ action,$ user,$ request)方法或matchCustom($action)方法来设置适当的模型方案.希望这会有所帮助.
还有一个选择:
覆盖控制器的runAction($id, $params = [])方法.这里$ id是actionID - 正是你需要的.检查id,设置适当的模型场景并调用parent::runAction($id, $params);