8

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
  • 327
  • 1
  • 6
  • 16

3 Answers3

18

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.

Community
  • 1
  • 1
GAMITG
  • 3,810
  • 7
  • 32
  • 51
  • well, 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? – Tariq Albajjali Dec 09 '15 at 10:57
  • would you check following [link](http://stackoverflow.com/questions/33036766/yii2-isguest-giving-exception-in-console-application) ? – GAMITG Dec 09 '15 at 11:22
  • Actually i did, i have the same config but still the session error appears – Tariq Albajjali Dec 09 '15 at 11:24
  • you want to remove session in console? if yes so, just remove `session` component from `components` array. – GAMITG Dec 09 '15 at 11:26
  • well, i don't need the session in console, i tried to remove it from config but still getting the same error. – Tariq Albajjali Dec 09 '15 at 11:32
  • You need to clear cache or runtime asset of application. – GAMITG Dec 09 '15 at 11:39
  • do you have any link that explain this process, i googled it but it seems that i have to delete the run time files. – Tariq Albajjali Dec 09 '15 at 13:07
  • just delete all folders from runtime folder and also delete asset's inner folder. – GAMITG Dec 10 '15 at 04:13
  • Did 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 Dec 28 '21 at 15:23
  • I found the answer. See kurmi's response below for the rest of the fix. – Tanoro Dec 28 '21 at 17:39
6

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

kurmi
  • 448
  • 4
  • 6
  • A 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 Dec 28 '21 at 17:38
1

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;
Richard Li
  • 49
  • 4