在yii2-app-base中使用DbManager配置authManager

时间:2015-09-30 13:29:01

标签: php yii2 rbac yii2-basic-app

我正在通过Security-Authorization Tutorial使用DbManager配置authManager。

web.php

中声明以下代码后
<?php

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

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
    'urlManager' => [
      'showScriptName' => false,
      'enablePrettyUrl' => true
    ], 
    'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'tYXyeisqgn9Qn_baaI6JRV4a6NY54nrq',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
        'viewPath' => '@backend/mail',
            'useFileTransport' => true,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'localhost',
            'username' => 'root',
            'password' => '',
            'port' => '8080',
            'encryption' => 'tls',
                        ],
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
    ];
}

return $config;

此代码在 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' => [
    'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
    ],
    'params' => $params,
];

配置/ db.php中

<?php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=danishYii',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8',
];

我输入了./yii migrate --migrationPath=vendor/yiisoft/yii2/rbac/migrations 在我的终端。这个命令来自Component is not getting loaded -Stack overflow

我在终端

中收到此错误
  

异常&#39; yii \ db \ Exception&#39;消息&#39; SQLSTATE [HY000] [2002]   无法通过套接字连接到本地MySQL服务器   &#39; /var/run/mysqld/mysqld.sock' (2)&#39;

enter image description here

我对Yii很新。所以请不要介意这是一个愚蠢的问题。 请帮我纠正这个问题。

1 个答案:

答案 0 :(得分:2)

检查是否在您的

console/config/main.php 

您有正确的数据库访问配置,例如yii2-app-advanced template

的此示例
'components' => [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=your_hostname;dbname=your_dbname',
        'username' => 'your_username',
        'password' => 'your_password',
        'charset' => 'utf8',
        'enableSchemaCache' => true,            
    ],

用于基本模板

请确保你在basic / console / config.php中有一个像db这样的组件部分的引用

return [
     .......
    'components' => [
         ......
    'db' => require(__DIR__ . '/db.php'),
    ],
      ......
];

然后在basci / config / db.php中使用正确的db config

    return [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=your_hostname;dbname=your_dbname',
        'username' => 'your_username',
        'password' => 'your_password',
        'charset' => 'utf8',
    ];