Problems with Yii2 Rules creation

2019-07-21 18:13发布

I have a Yii2 with Yii2 admin, user andAdminLTE installed. My problem is I don't know how to create rules, actually I don't know how to define the Class Name. Where "Classes" should be defined? How can I see which Classes do I have or add Classes?

Rules creation

Thanks a lot,

标签: yii2
1条回答
放荡不羁爱自由
2楼-- · 2019-07-21 19:05

I don't know what is that module that you use but i know how to define rules for controller.please open a controller for example : mycontroller. when you want create a rule for action in your mycontroller you should use the 'behaviors' function as you can see in bellow.

   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' => ['@'],
                ],
        ],
        ],

    ];
}

i have two actions in this controller 'signup','logout'and i give roles to every actions.i give ? role to sign up and @ role to logout. ? roles:means every user with out login can see this action. @ roles:means every user with login cans see this action. as you can see the class in rules definition is static and you don't need to specify class you can just can use AccessControl::className() in your code. best regards

查看更多
登录 后发表回答