Getting unknown property: yii\console\Application::session while run ./yii - php

I want to run yii2 console command, then I test it with run this ./yii
When I run ./yii I got this response
Exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: yii\console\Application::session'
in /var/www/html/myweb/vendor/yiisoft/yii2/base/Component.php:143
Stack trace:
#0 /var/www/html/myweb/vendor/yiisoft/yii2/di/ServiceLocator.php(73): yii\base\Component->__get('session')
#1 /var/www/html/myweb/vendor/kartik-v/yii2-grid/Module.php(62): yii\di\ServiceLocator->__get('session')
Here is my common/config/params-local.php
return [
'uploadPath' => __DIR__ .'/../../uploads/',
'baseurl' => 'http://localhost/myweb/'
];
Here is my common\config\params.php
<?php
return [
'adminEmail' => 'no-reply#myweb.com',
'supportEmail' => 'no-reply#myweb.com',
'user.passwordResetTokenExpire' => 3600,
];
Here is my console\config\params-local.php
<?php
return [
];
Here is my console\config\params.php
<?php
return [
'adminEmail' => 'no-reply#myweb.com',
];
Here is my common\config\main.php
<?php
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
],
'modules' => [
'redactor' => [
'class' => 'yii\redactor\RedactorModule',
'uploadDir' => __DIR__ .'/../../uploads/konten',
'uploadUrl' => '/myweb/uploads/konten',
'imageAllowExtensions'=>['jpg','png','gif']
],
'gridview' => [
'class' => '\kartik\grid\Module',
]
],
];
Here is my common\config\main-local.php
<?php
return [
'language' => 'en-US',
'sourceLanguage' => 'id-ID',
'components' => [
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'google' => [
'class' => 'yii\authclient\clients\Google',
'clientId' => 'xxxxx-cppd86jm9qfrt77pc684pau01nilf261.apps.googleusercontent.com',
],
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',
'clientId'=> 'xxxxxx16917400',
'clientSecret' => 'xxxxxx8d99ff80ce1f713424',
],
],
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=192.168.0.106;dbname=mydb',
'username' => 'dev',
'password' => 'dev123',
'charset' => 'utf8',
'enableSchemaCache' => false,
'schemaMap' => [
'pgsql'=> [
'class'=>'yii\db\pgsql\Schema',
'defaultSchema' => 'public2' //specify your schema here
]
], // PostgreSQL
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
// 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,
],
'mail' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#backend/mail',
'useFileTransport' => false,//set this property to false to send mails to real email addresses
//comment the following array to send mail using php's mail function
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'iix70.hosting.com',
'username' => 'myuser',
'password' => 'mypass',
'port' => '465',
'encryption' => 'ssl',
],
],
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '../../messages',
'sourceLanguage' => 'id-ID',
'fileMap' => [
'app' => 'app.php',
],
],
],
],
]
];
Looks like something wrong with my script.
Currently i'm using ubuntu.
What should I do next in case to fix that? so it should response with yii command list instead of error.
and what cause these error?
Thanks in advance.

When you add a value to common/config folder files, configurations used in all applications like backend, frontend, console, api and others. So in advanced template, you must just add values which are related to all these applications. Based on documentation common folder is files common to all applications. This picture shows it clearly:
For your problem, as others mentioned, you don't have any session in console, but you added or used this module in common/config/params-local.php and based on introduction of this answer, it will be used in console/config/params-local.php and you get an error :).
Update: Based on your updated question, your common/config/main.php file is:
<?php
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
],
'modules' => [
'redactor' => [
'class' => 'yii\redactor\RedactorModule',
'uploadDir' => __DIR__ .'/../../uploads/konten',
'uploadUrl' => '/myweb/uploads/konten',
'imageAllowExtensions'=>['jpg','png','gif']
],
'gridview' => [
'class' => '\kartik\grid\Module',
]
],
];
gridviw module, implicitly uses session for saving state of sorting. In other side you added this to config folder of common, so based on previous notes, it also will be used in console application. Console doesn't have session (and I think you don't need a grid view in your console :D) and it causes an error. For solving this problem, move this lines
'modules' => [
'redactor' => [
'class' => 'yii\redactor\RedactorModule',
'uploadDir' => __DIR__ .'/../../uploads/konten',
'uploadUrl' => '/myweb/uploads/konten',
'imageAllowExtensions'=>['jpg','png','gif']
],
'gridview' => [
'class' => '\kartik\grid\Module',
]
],
to main.php of frontend or backend folder (based on your situations and use).

Related

Migration - Failed to instantiate component or class "db" in console Yii2

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();
},
];

Turning on PageCache in yii2 produces strange encoding problem

I have a web portal that I want to add PageCache to. When Activating it, a strange coding appears and after debugging for a while I don't understand the reason since in other projects developed in Yii2 the PageCache works correctly.
[
'class' => 'yii\filters\PageCache',
'only' => ['index'],
'duration' => 300,
'enabled'=>true,
'variations' => [
\Yii::$app->language,
],
'dependency' => [
'class' => 'yii\caching\DbDependency',
'sql' => 'SELECT COUNT(*) FROM item',
],
],
Web config
$config = [
'id' => 'my-web',
'language'=>'en',
'name'=>'My Web',
'basePath' => dirname(__DIR__),
'defaultRoute' => 'site/index',
'bootstrap' => ['log'],
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
'components' => [
'request' => [
'cookieValidationKey' => 'XXXXXXXXXXXXXX',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_cookiename', 'httpOnly' => true],
],
'session' => [
'name' => 'session-identifier',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
],
'params' => $params,
];
Ubuntu: Ubuntu 16.04.2 LTS
Mysql: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)
php: 7.0.18
Cache content snippet
^_<8b>^H^#^#^#^#^#^#^Cí=ÛvÛ6¶ïý
X<9d><99>$§¦Dêfɱ<9d>ºN<9c>fN.<9e>ÆM<93>^V^VDB^Rl<92>` Ò<96>ÓéÇôq^^æá¬y<9b><97>®ÕüØÙ^#x'%Q<8a>SÛ­<95>^UY$.^[ØØwÜv6^^¾88~sô^HM^CÇÞûdGüA6v'»^MÂ^[{<9f> øìL ¶ÔOùè<90>^##s<8a>}N<82>ÝÆ×Ç<87>Ú Ñ*¦»Ø!»<8d>3JÎ=æ^G^Md27 .ä?§V0ݵÈ^Y5<89>&^_6^Qui#±­q^SÛd×ØD^N<9e>Q't¢^W½¦¾<89>BN|ù<8c>GðJo¤^#+!<9b>Ü^_k^^ö±<93><81>ýV¼<85><92>¥<8c>^A;%n&ã^[ûô5?^\¿èøüõÁ´£=yr|pö<90><85>m^]^?«^]q|ôz<82>ñ<81>ñTÿö^_úQçéøY<97>¾~wñ¤û^Of|^[|K<9f>^?ó<9a>¾6IçèâÕ³ÿýû^E^?F¦ôéì|w7Óê<9d><80>^F6Ù{é`?xÆΨ<8d>4ô^U^Q-6éûÿ¸( öû^?<8d><99>Ë8rÞÿ^GÒ ßD¾Ìà#z4e.<81>^R/^A^_^NvM¼ÓRõ¥õÛÔ=<85>^B6t^Q»Ì¥<80>º^F<9a>úd¼Û<98>^F<81>·Ýj^YÃvÓè^O<9a><9d>NÓÐ[<8d>ʲØ^N<88>ïâ<80>¨² i,¬ Í!^G^\^FSægð<9c>ö~ÙxZ<84><9b>>õ^Bʲã^Tã£^BY<88>Ǩi&xe.r<98>J^M^D^Y^E<80>RA<9e>ï^? äË^Kä<84>æ<94>A^]¿ð&:^#(Ø<81>*^A^\^Bê^C<8a>¥^LY$^Z^E5Ldæù<84>ó^XF<4<8d>j^DL^X<9b>ØDã4 Ú^Yñé^XF¥Ð¥Ù<9b>Wçmã^[»ëôÂCþhöôHcìø^_o»Ý§£·§^G§Ó·xüÎÖ^O¼Çæ\<;ü^LÛÔ<82>QkêF¦î~ça»ß3<8c>n{h|qð¨×ë^_^ZíÃîðàáaGï^Z<83>\<85><85><9a>=<9f>yÄ^O.v^[l²m3Á<98><99><9a ......
I added several screenshots
What can be the reason for this behavior?

Yii2 - Working With Multiple backend- session and cookies

Let me tell you the case.
Basically I have separate backend in yii2 advanced template.
Why ? This is the reason
My office have a lot of branch office in a country with a lot of departements of each branch.
This departements, I have interpretation of them as modules.
The departement name is same but sometime, they have a lot of different
behaviours.
As example admin in headquarters can erase employee name in branch
office, but admin branch office , they can not.
So, I choose to separate them into backend folder each like this :
backend (which is portal branch and also super-admin backend)
-modules
-human_resource
backend-jkt (which is Jakarta Indonesia backend)
-modules
-human_resource
My question is :
When user successfully login to backend, then i created a link to backend-jkt, it's automatically login also.
As vice versa,
When people directly to backend-jkt but not logged in to backend, it's automatically redirect to backend's login,
Now my situation is: when user logged in to backend, then click link "Jakarta" as above in image, user have to sign in again.
This is my config in backend
<?php
$params = array_merge(
require __DIR__ . '/../../common/config/params.php',
require __DIR__ . '/../../common/config/params-local.php',
require __DIR__ . '/params.php',
require __DIR__ . '/params-local.php'
);
return [
'id' => 'app-backend',
'name' => 'Backend System',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [
'mimin' => [
'class' => '\hscstudio\mimin\Module',
],
'SuperAdmin' => [
'class' => 'backend\modules\super_admin\SuperAdmin',
],
],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_identity-backend',
'httpOnly' => true
],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
'savePath' => sys_get_temp_dir(),
],
'request' => [
'cookieValidationKey' => 'IkR77lm93Rcb9TCoYTAZ',
'csrfParam' => '_csrf-backend',
],
'assetManager' => [
'bundles' => [
'dmstr\web\AdminLteAsset' => [
],
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'suffix' => '.html',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
'urlManagerBackendJkt' => [
'class' => 'yii\web\urlManager',
'baseUrl' => '/backend-jkt/web/',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'http://jkt.tresnamuda.local/' => '#app/index',
],
],
'authManager' => [
'class' => 'yii\rbac\DbManager', // only support DbManager
],
],
'as access' => [
'class' => '\hscstudio\mimin\components\AccessControl',
'allowActions' => [
// add wildcard allowed action here!
'site/*',
'debug/*',
// 'mimin/*', // only in dev mode
],
],
'params' => $params,
];
And this is the backend-jkt
<?php
$params = array_merge(
require __DIR__ . '/../../backend/config/params.php',
require __DIR__ . '/../../backend/config/params-local.php',
require __DIR__ . '/params.php',
require __DIR__ . '/params-local.php'
);
return [
'id' => 'app-backend_jkt',
'name' => 'Jkt Backend System',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend_jkt\controllers',
'bootstrap' => ['log'],
'modules' => [
'mimin' => [
'class' => '\hscstudio\mimin\Module',
],
],
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_identity-backend',
'httpOnly' => true
],
],
'session' => [
// this is the name of the session cookie used for login on the backend
'name' => 'advanced-backend',
'savePath' => sys_get_temp_dir(),
],
'request' => [
'cookieValidationKey' => 'IkR77lm93Rcb9TCoYTAZ',
'csrfParam' => '_csrf-backend',
],
'assetManager' => [
'bundles' => [
'dmstr\web\AdminLteAsset' => [
],
],
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'suffix' => '.html',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
'authManager' => [
'class' => 'yii\rbac\DbManager', // only support DbManager
],
],
'as access' => [
'class' => '\hscstudio\mimin\components\AccessControl',
'allowActions' => [
// add wildcard allowed action here!
'site/*',
'debug/*',
// 'mimin/*', // only in dev mode
],
],
'params' => $params,
];
your question about cookies that place in user's browsers seprate by domain and Path , so you have to store it for next domain Path , I recommend to you after clicking Jakarta send user-id and private-key to Jakarta and there force login that user-id by simple command :
if(private-key is Okey and you get $user-id by POST ) {
$user = User::findOne($user-id);
Yii::$app->getUser()->login($user);
}
private-key is simple or advance why that you can increase your security , you may leave it and just check have user-id or not !

Setting unknown property: yii\console\ErrorHandler::errorAction on linux server

I just uploaded my yii advanced project to my centos server, but I can't seem to get past the migrate phase. When I try to run yii migrate the following error occurred:
`Setting unknown property: yii\console\ErrorHandler::errorAction'
I have no idea why this happens, because it works fine when I run it locally on my windows computer.
My yii advance project is bit different than a normal Yii advanced. The backend has been separated from the frontend so it just contains the console and frontend directory.
common/config/main.php
$config = require(__DIR__ . '/main-console.php');
array_push($config['bootstrap'], 'site');
$config['components']['errorHandler'] = [
'errorAction' => 'site/error',
];
$config['components']['user'] = [
'identityClass' => 'frontend\models\User',
'enableAutoLogin' => true,
];
$config['components']['session'] = [
'name' => 'PHPFRONTSESSID',
'savePath' => sys_get_temp_dir(),
];
$config['components']['request'] = [
'cookieValidationKey' => 'IBzCJMjLWUaXMZemYUej',
'csrfParam' => '_frontendCSRF',
];
$config['components']['site'] = [
'class' => 'frontend\components\SiteComponent',
];
return $config;
main-console.php
$params = array_merge(
require(__DIR__ . '/params.php')
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log','debug'],
'sourceLanguage' => 'en-US',
'controllerNamespace' => 'frontend\controllers',
'aliases' => [
'#local_media' => '#frontend/web/uploads/media',
],
'modules' => [
'debug' => [
'class' => 'yii\debug\Module',
],
],
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#frontend/messages',
],
],
],
'assetManager' => [
'bundles' => false,
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning', 'trace'],
],
],
],
'defaultRoute' => 'site/view',
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => true,
'enableStrictParsing' => false,
'rules' => require('routes.php'),
],
],
'params' => $params,
];
Can someone give me some advies on how to solve this problem?
You problem is that you specify error action into common/config/main.php. Error action must be used only with web apps, not console. So move this to your frontend and backend configs separately:
$config['components']['errorHandler'] = [
'errorAction' => 'site/error',
];
There is no errorAction attribute in yii\console\ErrorHandler class. There is one in yii\web\ErrorHandler though. I'm not sure why this works on your local machine because it shouldn't. I guess some other configuration is in place there.

Error installation extentions Reportico on Yii 2

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',
]
]
]

Categories