php - Configuring authManager using DbManager in yii2-app-base -
i'm going through security-authorization tutorial configure authmanager using dbmanager.
after declaring below code in 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 secret key in following (if empty) - required 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 mails file default. have set // 'usefiletransport' false , configure transport // mailer 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 'dev' environment $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = [ 'class' => 'yii\debug\module', ]; $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii\gii\module', ]; } return $config;
and, code in 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, ];
config/db.php
<?php return [ 'class' => 'yii\db\connection', 'dsn' => 'mysql:host=localhost;dbname=danishyii', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ];
i typed ./yii migrate --migrationpath=vendor/yiisoft/yii2/rbac/migrations
in terminal. command got component not getting loaded -stack overflow
i got error in terminal
exception 'yii\db\exception' message 'sqlstate[hy000] [2002] can't connect local mysql server through socket '/var/run/mysqld/mysqld.sock' (2)'
i'm new yii. please don't mind if silly question. please me rectify problem.
check if in
console/config/main.php
you have proper configuration db access sample 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, ],
for basic template
be sure have in basic/console/config.php reference db in component secion this
return [ ....... 'components' => [ ...... 'db' => require(__dir__ . '/db.php'), ], ...... ];
and in 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', ];
Comments
Post a Comment