我有AdminController
和behavior
:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login'],
'allow' => true,
'roles' => ['*'],
],
[
'actions' => ['index', 'logout'],
'allow' => true,
'roles' => ['admin', 'editor', 'expert'],
],
[
'actions' => ['update', 'delete'],
'allow' => true,
'roles' => ['admin'],
]
]
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['get'],
],
],
];
}
然后我为以下项创建子控制器:
如何在父级(AdminController
)上使用规则,然后使其起作用?
如果我在deafult
中添加类似的规则,则可以,但是在全球范围内无效。
P.S。我做管理面板,然后要:
-任何人都可以尝试登录
-访问管理窗格:['admin', 'editor', 'expert']
-注销只能['admin', 'editor', 'expert']
这是具有父级admin
的所有模块AdminController
的全局规则。
谢谢。
答案 0 :(得分:0)
如果扩展控制器,然后覆盖behaviors()
,则请确保包括以下家长行为:
return ArrayHelper::merge(parent::behaviors(), [
// your behaviors here
]);
答案 1 :(得分:0)
完成! 我将AdminController设置为:
class AdminController extends Controller {
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['admin', 'editor', 'expert'],
],
[
'actions' => ['login'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['delete'],
'allow' => true,
'roles' => ['admin'],
]
]
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['get'],
],
],
];
}
}
并为管理员中的每个控制器扩展它