Yii2 - Getting unknown property: yii\console\Application::user

╄→гoц情女王★ 提交于 2019-12-09 16:43:25

问题


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?


回答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.




回答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




回答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;


来源:https://stackoverflow.com/questions/34174750/yii2-getting-unknown-property-yii-console-applicationuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!