Yii2 - 获取未知属性:yiiconsoleApplication::user

我正在尝试从终端运行控制台控制器,但我每次都收到此错误

I am trying to run a console controller from the terminal, but i am getting this errors every time

Error: Getting unknown property: yiiconsoleApplication::user

这里是控制器

class TestController extends yiiconsoleController {

public function actionIndex() {
    echo 'this is console action';
} }

这是concole配置

and this is the concole config

return [
'id' => 'app-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'consolecontrollers',
'modules' => [],
'components' => [
    'log' => [
        'targets' => [
            [
                'class' => 'yiilogFileTarget',
                '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

有人可以帮忙吗?

推荐答案

控制台应用程序没有 Yii->$app->user.所以,你需要在configconsole.php中配置user组件.

Console application does not have Yii->$app->user. So, you need to configure user component in configconsole.php.

喜欢,

configconsole.php

 'components' => [
 .........
 ......
        'user' => [
            'class' => 'yiiwebUser',
            'identityClass' => 'appmodelsUser',
            //'enableAutoLogin' => true,
        ],
        'session' => [ // for use session in console application
            'class' => 'yiiwebSession'
        ],
 .......
]

有关您的问题的更多信息,请参阅:链接

More info about your problem see this : Link

访问以下链接:Yii2 isGuest 在控制台应用程序中给出异常

注意:控制台应用程序中没有会话.

Note : There's no session in console application.

相关文章