A
A
Alexey Voropaev2016-06-13 00:17:49
Yii
Alexey Voropaev, 2016-06-13 00:17:49

How to implement rbac dbmanager in yii2-advanced?

In basic, it turned out to implement RBAC. Today I started to study advanced and I can't implement RBAC.
How I do:
Step 1: Registered in /common/config/main.php

'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
    ],

Step 2: Run the migration. Tables added
php yii migrate [email protected]/rbac/migrations/

Step 3: In /console/controllers/ created the class RbacController.php
<?php

namespace yii\console\controllers;

use Yii;
use yii\console\Controller;
use common\models\User;

class RbacController extends Controller {

    public function actionInit() {
        $auth = Yii::$app->authManager;

        // Удаляем старые данные из бд
        $auth->removeAll(); 
        
        // Создадим роли админа и пользователя
        $admin = $auth->createRole('admin');
        $user = $auth->createRole('user');
        
        // запишем их в БД
        $auth->add($admin);
        $auth->add($user);
        
        // Назначаем роль admin пользователю с ID 1
        $auth->assign($admin, 1);
}

Step 4: I execute in the console php yii rbac/init . An error is displayed
Exception 'yii\base'UnkownClassException' with message 'Unable to find 'console\controllers\RbacController.php' in file: D:\domains\yii2/console/controllers\RbacController.php. Namespace missing?'

in D:\Openserver\domains\yii2\vendor\yiisoft\yii\BaseYii.php:291

What am I doing wrong? maybe I wrote something wrong in RbacController.php or missed some step?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2016-06-13
@boomrap

You have a controller in the console/commands namespace, but you need it to be in console/controllers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question