在 yii2 basic 中未加载组件 [英] component is not getting loaded in yii2 basic
问题描述
我使用的是 yii2 基本模板.我正在尝试使用 yii2 中的数据库迁移创建 RBAC DB,我成功创建了几个表,在创建 RBAC 表时,我得到了不同的功能,如下所示:
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;
}
即使我添加了这个函数,这个函数也不会返回 authManager
This function is not returning authManager even though I have added this
'authManager'=>[
'class'=>'yii\rbac\DbManager',
],`
在控制台.php
前几行错误是:
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()
console.php 代码
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,
];
推荐答案
好的,你的回答我明白了:你可能复制了 原始 RBAC 迁移脚本,但忘记包含 use
行在文件的顶部,这可能吗?
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?
由于缺少 use
不知道 DbManager
的实际含义,所以 getAuthManager()
检查未知类和条件失败.这也解释了为什么通过添加 use yii\rbac\DbManager
解决了问题.
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
.
话虽如此,运行框架迁移的正确方法是简单地使用 migrationPath
选项:
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 :)
这篇关于在 yii2 basic 中未加载组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!