在Yii2中使用控制器中的beforeAction()和behavior()方法

Yas*_*tel 2 php yii yii2 yii2-advanced-app yii2-behaviors

我想在我的控制器中使用both beforeAction()behaviors()method.

如果我beforeAction()在我的代码中添加方法比behaviors()方法不起作用.

如果我删除 beforeAction()方法比behaviors()方法工作.

我不想删除,beforeAction()因为它用于禁用ajax调用的csrf令牌.

public function beforeAction($action)
{
  if($action->id =='ignore' || $action->id =='accept')
  {
    $this->enableCsrfValidation = false;
  }
  return true;
}
Run Code Online (Sandbox Code Playgroud)

我想使用behaviors()方法进行身份验证.

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['create','index','update','change','view','page','active','list'],
            'rules' => [
                [
                    'actions' => ['create','index','update','change','view','page','active','list'],
                    'allow' => true,
                    'roles' => ['@'],
                    'matchCallback' => function ($rule, $action)
                    {
                      echo "string";
                      die;
                    },
                ],
            ],
            'denyCallback' => function ($rule, $action) {
                return $this->redirect(Yii::$app->request->baseUrl);
            }
        ],
    ];
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在同一个控制器中使用这两种方法.

Kan*_*tel 11

public function beforeAction($action)
{
  if($action->id =='ignore' || $action->id =='accept')
  {
    $this->enableCsrfValidation = false;
  }
  //return true;
  return parent::beforeAction($action);
}
Run Code Online (Sandbox Code Playgroud)

你需要在beforeAction()之前返回父级