component is not getting loaded in yii2 basic

大城市里の小女人 提交于 2019-12-13 04:12:05

问题


I am using yii2 basic template. I am trying create RBAC DB using database migration in yii2, I have created few tables successfully and while I am creating RBAC tables I have got different function as show below:

protected function getAuthManager()
{
    $authManager = Yii::$app->getAuthManager();
    if (!$authManager instanceof DbManager) {
        throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
    }
    return $authManager;
}

This function is not returning authManager even though I have added this

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

in console.php

first few lines of error is:

Exception: You should configure "authManager" component to use database before executing this migration. (C:\xampp\htdocs\PMTool\migrations\m150820_064854_rbac.php:13)
 C:\xampp\htdocs\PMTool\migrations\m150820_064854_rbac.php(20): m150820_064854_rbac->getAuthManager()

code of console.php

`<?php

Yii::setAlias('@tests', dirname(__DIR__) . '/tests');

$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');

return [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'app\commands',
'modules' => [
    'gii' => 'yii\gii\Module',
],
'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
    'log' => [
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
            ],
        ],
    ],
    'db' => $db,
    'authManager'=>[
        'class'=>'yii\rbac\DbManager',
    ],
],
'params' => $params,
];

回答1:


I have resolved the issue.By including DbManager in migrations class as below

use yii\rbac\DbManager;




回答2:


Okay, with your answer I get it: You probably copied the class of the original RBAC migration script, but forgot to include the use lines on the top of the file, is that possible?

Since the use is missing it is not known what DbManager actually means, so getAuthManager() checks against an unknown class and the condition fails. This also explains why the problem was solved by you adding the use yii\rbac\DbManager.

That being said, the correct way to run the framework migrations is by simply using the migrationPath option:

./yii migrate --migrationPath=vendor/yiisoft/yii2/rbac/migrations

I would say, try that and it would cause you a whole lot less problems :)



来源:https://stackoverflow.com/questions/32112290/component-is-not-getting-loaded-in-yii2-basic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!