Yii2 规则创建问题 [英] Problems with Yii2 Rules creation

查看:37
本文介绍了Yii2 规则创建问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安装了 Yii2 管理员、用户和 AdminLTE 的 Yii2.我的问题是我不知道如何创建规则,实际上我不知道如何定义类名.应该在哪里定义类"?如何查看我有哪些课程或添加哪些课程?

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?

非常感谢,

推荐答案

我不知道你使用的那个模块是什么,但我知道如何为控制器定义规则.请打开一个控制器,例如:mycontroller.当您想在 mycontroller 中创建操作规则时,您应该使用行为"功能,如下所示.

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

    ];
}

我在这个控制器中有两个动作注册",注销",我给每个动作分配角色.我给?角色注册和@角色注销.?角色:意味着每个没有登录的用户都可以看到这个动作.@角色:表示每个登录的用户都可以看到这个动作.如您所见,规则定义中的类是静态的,您无需指定类,您只需在代码中使用 AccessControl::className() 即可.最好的问候

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

这篇关于Yii2 规则创建问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆