Yii2 - Getting unknown property: yii\console\Application::user
Solution 1
Console application does not have Yii->$app->user
. So, you need to configure user
component in config\console.php
.
like as,
config\console.php
'components' => [
.........
......
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\User',
//'enableAutoLogin' => true,
],
'session' => [ // for use session in console application
'class' => 'yii\web\Session'
],
.......
]
More info about your problem see this : Link
OR
Visit following link : Yii2 isGuest giving exception in console application
Note : There's no session in console application.
Solution 2
Set in \console\config\main.php
return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers',
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\Credential',// class that implements IdentityInterface
//'enableAutoLogin' => true,
],
],
'params' => $params,
];
now in your \console\controller\AbcController.php add init method
public function init() {
parent::init();
Yii::$app->user->setIdentity(Credential::findOne(['id'=><cronloginid>]));
}
create a cron login and pass that login id in variable with this config your Blameable Behavior of yii2 will work
Solution 3
As @GAMITG said, you must config user component in config file, but unfortunately, you couldn't access session in console, that's because session is not available in console. Maybe you could solve the problem like this:
$user_id = isset(Yii::$app->user->id) ? Yii::$app->user->id : 0;
Tariq Albajjali
Updated on July 28, 2022Comments
-
Tariq Albajjali over 1 year
I am trying to run a console controller from the terminal, but i am getting this errors every time
Error: Getting unknown property: yii\console\Application::user
here is the controller
class TestController extends \yii\console\Controller { public function actionIndex() { echo 'this is console action'; } }
and this is the concole config
return [ 'id' => 'app-console', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'controllerNamespace' => 'console\controllers', 'modules' => [], 'components' => [ 'log' => [ 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], ], ], ], 'params' => $params];
I tried running it using these commands with no luck
php yii test/index php yii test php ./yii test
can anyone help please?
-
Tariq Albajjali over 8 yearswell, thanks it solved the user error but now am getting the session error yii\console\Application::getSession(), i tried to remove it since there is no sessions in console but still getting the same error. any ideas?
-
GAMITG over 8 yearswould you check following link ?
-
Tariq Albajjali over 8 yearsActually i did, i have the same config but still the session error appears
-
GAMITG over 8 yearsyou want to remove session in console? if yes so, just remove
session
component fromcomponents
array. -
Tariq Albajjali over 8 yearswell, i don't need the session in console, i tried to remove it from config but still getting the same error.
-
GAMITG over 8 yearsYou need to clear cache or runtime asset of application.
-
Tariq Albajjali over 8 yearsdo you have any link that explain this process, i googled it but it seems that i have to delete the run time files.
-
GAMITG over 8 yearsjust delete all folders from runtime folder and also delete asset's inner folder.
-
Tanoro over 2 yearsDid this solution ever work? I have a console script that is throwing the same yii\console\Application::getSession() error and deleting the contents of the runtime directory hasn't fixed it.
-
Tanoro over 2 yearsA little out of date, but worked perfectly! I'm writing a console command that imports data into the application and I wanted to use the models that already exist to do my validations and inserts, but these models need to know what user account is performing actions. I needed to simulate a "sessionless" login in the console so all models had scope of Yii::$app->user->identity. Thanks!
-
Tanoro over 2 yearsI found the answer. See kurmi's response below for the rest of the fix.