创建Yii2规则时遇到的问题
我有一个安装了Yii2管理员、用户和管理员的Yii2。我的问题是我不知道如何创建规则,实际上我不知道如何定义类名。应在何处定义“类”?如何查看我有哪些类或添加哪些类创建Yii2规则时遇到的问题,yii2,Yii2,我有一个安装了Yii2管理员、用户和管理员的Yii2。我的问题是我不知道如何创建规则,实际上我不知道如何定义类名。应在何处定义“类”?如何查看我有哪些类或添加哪些类 非常感谢,我不知道您使用的模块是什么,但我知道如何为控制器定义规则。请打开控制器,例如:mycontroller。 当您想要在mycontroller中创建操作规则时,您应该使用“行为”功能,如下面所示 class MyController extends Controller { public function beha
非常感谢,我不知道您使用的模块是什么,但我知道如何为控制器定义规则。请打开控制器,例如:mycontroller。 当您想要在mycontroller中创建操作规则时,您应该使用“行为”功能,如下面所示
class MyController extends Controller {
public function behaviors() {
return [
'access' => [
//you can use this class is use for every controller "AccessControl::className()"
'class' => AccessControl::className(),
// use this rules just for these two actions(logout and signup)
'only' => ['logout', 'signup'],
//this is your rules for your controller's actions
'rules' => [
[
'actions' => ['signup'],
'allow' => true,
// '?' is the default roles in yii2
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
// '@' is the default roles in yii2
'roles' => ['@'],
],
],
],
];
}
我在这个控制器“注册”和“注销”中有两个动作,我给每个动作分配角色。我给?要注册的角色和要注销的@role。
? 角色:意味着每个未登录的用户都可以看到此操作。
@角色:表示每个登录的用户都可以看到此操作。
正如您所看到的,规则定义中的类是静态的,您不需要指定类,只需在代码中使用AccessControl::className()。
致意