是否可以在\yii\filters\AccessRule
中使用两个不同的规则集behaviors()
来控制不同的操作?像这样:
public function behaviors()
{
return [
// Standard access
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['create'],
'allow' => true,
'roles' => ['@'],
],
],
],
// fancy access
'accessView' => [
'class' => AccessControl::className(),
'ruleConfig' => [
'class' => MyFancyAccessRule::className(),
],
'rules' => [
[
'actions' => ['view'],
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
在这种情况下,我希望将不同的访问规则应用于view
操作,因为它需要额外的检查...
答案 0 :(得分:3)
是的,您可以像示例中一样多次附加相同的行为(AccessControl
是用于控制对指定操作的访问的行为。)
但是您不需要。您只能使用一种AccessControl
行为,并直接在rules
config中配置规则:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['create'],
'allow' => true,
'roles' => ['@'],
],
[
'class' => MyFancyAccessRule::className(),
'actions' => ['view'],
'allow' => true,
'roles' => ['@'],
],
],
],
];
}