1

我正在阅读安全授权教程以使用 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 在终端上输入。我从组件获得的这个命令没有被加载-堆栈溢出

我的终端出现此错误

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

在此处输入图像描述

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

4

1 回答 1

2

检查是否在您的

console/config/main.php 

你有一个正确的数据库访问配置,比如这个 yii2-app-advanced 模板的示例

'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 一个正确的数据库配置

    return [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=your_hostname;dbname=your_dbname',
        'username' => 'your_username',
        'password' => 'your_password',
        'charset' => 'utf8',
    ];
于 2015-09-30T14:21:09.683 回答