I am new to Yii2 and need to set up an existing project. I am at the stage where I would like to migrate tables to the database using the console. I have a database connection in Yii, everything in config is specified correctly. However, I get an error in the console when I try to migrate the tables:
Exception 'yiibaseInvalidConfigException' with message 'Failed to instantiate component or class "db".'
I have already searched Google and have not found a solution to this problem.
Here some code:
config/console.php
<?php
use kartik\mpdf\Pdf;
Yii::setAlias('#webdir', realpath(dirname(__FILE__) . '/../../'));
$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');
$config = [
'id' => 'easyads-console',
'basePath' => dirname(__DIR__),
'aliases' => [
'#assets' => realpath('../assets'),
'#webroot' => realpath('../'),
],
'bootstrap' => [
'log',
'app\yii\base\Settings',
],
'on beforeRequest' => ['\app\init\Application', 'consoleBeforeRequest'],
'controllerNamespace' => 'app\commands',
'modules' => [
'admin' => [
'class' => 'app\modules\admin\Module',
'defaultRoute' => 'admin',
],
],
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
'options' => [
'class' => '\twisted1919\options\Options'
],
'pdf' => [
'mode' => Pdf::MODE_UTF8,
'class' => Pdf::classname(),
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_BROWSER,
],
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => [
[
'pattern' => 'listing/index/<slug:[a-z0-9_\-]+>',
'route' => 'listing/index',
]
]
],
'generateInvoicePdf' => [
'class' => 'app\components\GenerateInvoicePdfComponent',
],
'sendInvoice' => [
'class' => 'app\components\SendInvoiceComponent',
],
'mailer' => [
'class' => 'app\yii\swiftmailer\Mailer',
],
'consoleRunner' => [
'class' => 'vova07\console\ConsoleRunner',
'file' => '#app/console.php'
],
'mailQueue' => [
'class' => 'app\components\mail\queue\MailQueueComponent',
],
'twigTemplate' => [
'class' => 'app\components\mail\template\MailTemplateComponent',
],
'mailSystem' => [
'class' => 'app\components\mail\MailSystemComponent',
],
'migration' => [
'class' => 'twisted1919\helpers\Migration',
],
'mutex' => [
'class' => 'yii\mutex\FileMutex',
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
if (is_file($file = __DIR__ . "/console-local.php")) {
$config = \yii\helpers\ArrayHelper::merge($config, require $file);
}
return $config;
config/db.php
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=database;port=3306;dbname=db',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'tablePrefix'=> 'ea_',
'on afterOpen' => function($event) {
$event->sender->createCommand('SET time_zone="+00:00"')->execute();
$event->sender->createCommand('SET NAMES utf8')->execute();
$event->sender->createCommand('SET SQL_MODE=""')->execute();
},
];
Related
When I try login using social media in Yii2 if google, facebook, twitter and GitHub error. I'm sure is true for my configuration on \basic_yii2\config\web.php. This my configuration :
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'google' => [
'class' => 'yii\authclient\clients\Google',
'clientId' => 'example',
'clientSecret' => 'example',
],
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'clientId' => 'example',
'clientSecret' => 'example',
],
'twitter' => [
'class' => 'yii\authclient\clients\Twitter',
'consumerKey' => 'example',
'consumerSecret' => 'example',
],
'github' => [
'class' => 'yii\authclient\clients\Github',
'clientId' => 'example',
'clientSecret' => 'example',
],
],
],
and this my configuration for \basic_yii2\controllers\siteConttroler.php :
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV ? 'testme' : null,
],
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallBack' => [$this, 'successCallBack'],
],
];
}
public function successCallBack($client)
{
// get user data from client
$userAttributes = $client->getUserAttributes();
print_r($userAttributes);
exit;
}
and this description error
The property name is successCallback not successCallBack:
'auth' => [
'class' => 'yii\authclient\AuthAction',
'successCallback' => [$this, 'successCallBack'],
],
I installed the extension reportico through composer and already web.php configuration file, but when I go into Reportico Admin Page error instead. The solution how ?
enter image description here
scrip web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'K1pkkP_iwACp933A03LotJ3AfRsyIb-D',
],
'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.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
],
'reportico' => [
'class' => 'reportico\reportico\Module' ,
'controllerMap' => [
'reportico' => 'reportico\reportico\controllers\ReporticoController',
'mode' => 'reportico\reportico\controllers\ModeController',
'ajax' => 'reportico\reportico\controllers\AjaxController',
]
],
'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;
Move reportico to components array:
'components' => [
//...
'reportico' => [
'class' => 'reportico\reportico\Module' ,
'controllerMap' => [
'reportico' => 'reportico\reportico\controllers\ReporticoController',
'mode' => 'reportico\reportico\controllers\ModeController',
'ajax' => 'reportico\reportico\controllers\AjaxController',
]
]
]
I have define a module named `admin' in yii2.
I have registered this module in web.php config file.
But when I try to access this I face with an error.
here is my web.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'language'=>'fa_ir',
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'SiMhYyhP2MZ_BAi321KfDHFk5uleQFa9',
],
'jdate' => [
'class' => 'jDate\DateTime'
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'modules' => [
'admin' => [
'class' => 'app\modules\admin\Module',
],
],
'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.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'pishevaris#gmail.com',
'password' => 'bhtk1368',
'port' => '587',
'encryption' => 'tls',
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
'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;
here is my Module class:
<?php
namespace app\modules\admin;
class Module extends \yii\base\Module
{
public $controllerNamespace = 'app\modules\admin\controllers';
public function init()
{
parent::init();
// custom initialization code goes here
}
}
but when I try to access mysite/admin/default/index I face The configuration for the "modules" component must contain a "class" element error
Please define module in out side of component. Like
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'language'=>'fa_ir',
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'SiMhYyhP2MZ_BAi321KfDHFk5uleQFa9',
],
'jdate' => [
'class' => 'jDate\DateTime'
],
'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.
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'pishevaris#gmail.com',
'password' => 'bhtk1368',
'port' => '587',
'encryption' => 'tls',
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
'db' => require(__DIR__ . '/db.php'),
],
'modules' => [
'admin' => [
'class' => 'app\modules\admin\Module',
],
],
'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;
Modules are different with Components, and you defined your module as a component by adding this to component array. you should write your module after or before component. like below:
['components'] => [],
['modules'] => [],
I'm going through Security-Authorization Tutorial to configure authManager using DbManager.
After declaring the 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 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;
And, this 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 my terminal. This command i got from Component is not getting loaded -Stack overflow
I got this error in my terminal
Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002]
Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)'
I'm very new to Yii. So please don't mind if this is a silly question.
Please help me to rectify this problem.
Check if in your
console/config/main.php
you have a proper configuration for db access like this sample for 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 you have in basic/console/config.php a reference to db in component secion like this
return [
.......
'components' => [
......
'db' => require(__DIR__ . '/db.php'),
],
......
];
and then in basci/config/db.php a properly db config
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=your_hostname;dbname=your_dbname',
'username' => 'your_username',
'password' => 'your_password',
'charset' => 'utf8',
];
I want to make a installation script for my app in yii2 and for that I want to redirect it to a defaultRoute='installation/index' but I am getting this namespace error when I have right namespace in my installation controller
Also I have a Installation model which does not extends to the activerecords and is used to get the user input values and perform some actions without the need of saving them into DB but it's directory is also not found.
Installation controller code:
namespace livecrm\controllers;
class InstallationController extends \yii\web\Controller
{
public function actionIndex()
{
return $this->render('index');
}
}
install-config.php:
$config = [
'id' => 'app-livecrm',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'defaultRoute' => '/installation/index',
'components' => [
'request' => [
'cookieValidationKey' => 'JDqkJaMgIITAKcsJY6yvLQdM9jf7WghX',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'livefactory\models\User',
'enableAutoLogin' => false,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
],
];
return $config;
config/main.php:
$params = array_merge(
require(__DIR__ . '/../../livefactory/config/params.php'),
require(__DIR__ . '/../../livefactory/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-livecrm',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'livecrm\controllers',
'bootstrap' => ['log'],
'modules' => [
'gii' => [
'class' => 'yii\gii\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*', '*'] // adjust this to your needs
],
'gridview' => [
'class' => 'kartik\grid\Module',
],
'liveobjects' => [
'class' => 'livefactory\modules\liveobjects\Module',
],
'pmt' => [
'class' => 'livefactory\modules\pmt\Module',
],
'user' => [
'class' => 'livefactory\modules\user\Module',
],
'sales' => [
'class' => 'livefactory\modules\sales\Module',
],
'customer' => [
'class' => 'livefactory\modules\customer\Module',
],
'product' => [
'class' => 'livefactory\modules\product\product',
],
'cron' => [
'class' => 'livefactory\modules\cron\Module',
],
],
'components' => [
'user' => [
'identityClass' => 'livefactory\models\User',
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'authManager'=>[
'class' => 'yii\rbac\DbManager',
'defaultRoles' =>['guest'],
],
'as access' => [
'class' => 'mdm\admin\components\AccessControl',
'allowActions' => [
'site/*', // add or remove allowed actions to this list
]
],
],
'params' => $params,
];
Seems like you are using basic application template.
The namespace of controller for your case should be:
namespace app\controllers\InstallationController;
The error message is very clear by the way and tells exactly about that.
Update: If you need namespace different than app\controllers you can change it through controllerNamespace property of yii\base\Applcation. For example you can add this to your config:
'controllerNamespace' => 'livecrm\\controllers',
Official docs:
$controllerNamespace