PHP code example of yiier / yii2-rbac

1. Go to this page and download the library: Download yiier/yii2-rbac library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yiier / yii2-rbac example snippets


return [
    'modules' => [
        'rbac' => [
            'class' => 'yiier\rbac\Module',
            // 'ignoreModules' => ['gii', 'debug', 'rbac'],
            // 'menus' => [
            //    'rbac' =>'RBAC',
            //    'permissions' =>'Permissions',
            //    'roles' =>'Roles',
            //    'users' =>'Users',
            //    'rules' =>'Rules',
            // ],
            // 'safeDelete' => false, // 默认为 true,当为 true 时,角色有用户时,不能直接删除角色,要先去掉角色的所有用户
            // 'rulesPath' => ['@app/rbac/rules'], // rules 路径
            // 'userClassName' => 'app\models\User',
            // 'idField' => 'id',
            // 'usernameField' => 'shop_name',
            // 'mainLayout' => '@app/views/layout/rbac.php',
            // 'isAdvanced' => true, // 如果使用的是高级模板
            // 'advancedConfigs' = [ //如果你还有其他应用需要加入控制
            //     [
            //           '@common/config/main.php',
            //           '@common/config/main-local.php',
            //           '@frontend/config/main.php',
            //           '@frontend/config/main-local.php',
            //      ],
            //      [
            //           '@common/config/main.php',
            //           '@common/config/main-local.php',
            //           '@api/config/main.php',
            //           '@api/config/main-local.php',
            //      ],
            //      [
            //           '@common/config/main.php',
            //           '@common/config/main-local.php',
            //           '@backend/config/main.php',
            //           '@backend/config/main-local.php',
            //      ],
            //    ],
        ],
    ],
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
    ],
];

'components' => [
    // ...
],
'as access' => [
    'class' => 'yiier\rbac\components\AccessControl',
],

'yiier.rbac.config' => [
    'cacheDuration' => 3600, // 缓存时间,单位是秒。默认是 30 天
    'superManId' => 12 // 拥有所有权限的用户 ID
    'freeAccessRoutes' => ['/site/login', '/site/signup', '/site/error', '/site/captcha', '/site/logout', '/order/*'], // 白名单 URL
]

echo  \yii\widgets\Menu::widget(
    [
        'options' => [
            'class' => 'sidebar-menu'
        ],
        'items' => [
            [
                'label' => Yii::t('app', 'Dashboard'),
                'url' => Yii::$app->homeUrl,
                'icon' => 'fa-dashboard',
                'active' => Yii::$app->request->url === Yii::$app->homeUrl
            ],
            \Yii::$app->getModule('rbac')->getInstance()->getItems() // add menu
        ]
    ]
);

<?= \yiier\rbac\helpers\Html::a(Yii::t('rbac', 'Clear Cache'), ['clear-cache']) 

echo  yiier\rbac\widgets\Menu::widget(
    [
        'options' => [
            'class' => 'sidebar-menu'
        ],
        'items' => [
            [
                'label' => Yii::t('app', 'Dashboard'),
                'url' => Yii::$app->homeUrl,
                'icon' => 'fa-dashboard',
                'active' => Yii::$app->request->url === Yii::$app->homeUrl
            ],
            \Yii::$app->getModule('rbac')->getInstance()->getItems() // add menu
        ]
    ]
);

echo  yiier\rbac\widgets\SpuerMenu::widget(
    [
        'options' => [
            'class' => 'sidebar-menu'
        ],
        'items' => [
            [
                'label' => Yii::t('app', 'Dashboard'),
                'url' => Yii::$app->homeUrl,
                'icon' => 'dashboard',
            ],
            [
                'label' => Yii::t('app', 'Create Product'), 
                'icon' => 'plus', 
                'url' => ['/product/create'], 
                'strict' => true // 严格模式
            ],
            \Yii::$app->getModule('rbac')->getInstance()->getItems() // add menu
        ]
    ]
);

php composer.phar 

php yii migrate --migrationPath=@yii/rbac/migrations/