Yii2控制台'类用户不存在'

时间:2016-03-30 23:33:36

标签: yii2 yii2-advanced-app

我试图从我的基本目录运行./yii。几个月前我使用了控制台应用程序,然后就可以了。我不确定发生了什么变化。

我收到以下错误:

Exception 'ReflectionException' with message 'Class user does not exist'

in /var/www/html/beta/fs2/vendor/yiisoft/yii2/di/Container.php:415

Stack trace:
#0 /var/www/html/beta/fs2/vendor/yiisoft/yii2/di/Container.php(415): ReflectionClass->__construct('user')
#1 /var/www/html/beta/fs2/vendor/yiisoft/yii2/di/Container.php(358): yii\di\Container->getDependencies('user')
#2 /var/www/html/beta/fs2/vendor/yiisoft/yii2/di/Container.php(151): yii\di\Container->build('user', Array, Array)
#3 /var/www/html/beta/fs2/vendor/yiisoft/yii2/di/Instance.php(154): yii\di\Container->get('user')
#4 /var/www/html/beta/fs2/vendor/yiisoft/yii2/di/Instance.php(128): yii\di\Instance->get(NULL)
#5 /var/www/html/beta/fs2/vendor/yiisoft/yii2/filters/AccessControl.php(98): yii\di\Instance::ensure('user', 'yii\web\User')
#6 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Object.php(107): yii\filters\AccessControl->init()
#7 [internal function]: yii\base\Object->__construct(Array)
#8 /var/www/html/beta/fs2/vendor/yiisoft/yii2/di/Container.php(372): ReflectionClass->newInstanceArgs(Array)
#9 /var/www/html/beta/fs2/vendor/yiisoft/yii2/di/Container.php(151): yii\di\Container->build('yii\filters\Acc...', Array, Array)
#10 /var/www/html/beta/fs2/vendor/yiisoft/yii2/BaseYii.php(344): yii\di\Container->get('yii\filters\Acc...', Array, Array)
#11 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Component.php(180): yii\BaseYii::createObject(Array)
#12 /var/www/html/beta/fs2/vendor/yiisoft/yii2/BaseYii.php(518): yii\base\Component->__set('as beforeReques...', Array)
#13 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Object.php(105): yii\BaseYii::configure(Object(yii\console\Application), Array)
#14 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Application.php(206): yii\base\Object->__construct(Array)
#15 /var/www/html/beta/fs2/vendor/yiisoft/yii2/console/Application.php(85): yii\base\Application->__construct(Array)
#16 /var/www/html/beta/fs2/yii(30): yii\console\Application->__construct(Array)
#17 {main}

我该如何解决这个问题?

更新

按照建议将参数添加到我的console\config\main.php文件后,我现在收到以下错误:

Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\console\Request::getUserIP()'

in /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Component.php:285

Stack trace:
#0 /var/www/html/beta/fs2/vendor/yiisoft/yii2/filters/AccessRule.php(105): yii\base\Component->__call('getUserIP', Array)
#1 /var/www/html/beta/fs2/vendor/yiisoft/yii2/filters/AccessRule.php(105): yii\console\Request->getUserIP()
#2 /var/www/html/beta/fs2/vendor/yiisoft/yii2/filters/AccessControl.php(118): yii\filters\AccessRule->allows(Object(yii\base\InlineAction), Object(yii\web\User), Object(yii\console\Request))
#3 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/ActionFilter.php(71): yii\filters\AccessControl->beforeAction(Object(yii\base\InlineAction))
#4 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#5 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Array, Object(yii\base\ActionEvent))
#6 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Module.php(607): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#7 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Controller.php(139): yii\base\Module->beforeAction(Object(yii\base\InlineAction))
#8 /var/www/html/beta/fs2/vendor/yiisoft/yii2/console/Controller.php(91): yii\base\Controller->runAction('', Array)
#9 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Module.php(455): yii\console\Controller->runAction('', Array)
#10 /var/www/html/beta/fs2/vendor/yiisoft/yii2/console/Application.php(167): yii\base\Module->runAction('', Array)
#11 /var/www/html/beta/fs2/vendor/yiisoft/yii2/console/Application.php(143): yii\console\Application->runAction('', Array)
#12 /var/www/html/beta/fs2/vendor/yiisoft/yii2/base/Application.php(375): yii\console\Application->handleRequest(Object(yii\console\Request))
#13 /var/www/html/beta/fs2/yii(31): yii\base\Application->run()
#14 {main}

这是我的主配置文件:

$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-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log', 'gii'],
    'controllerNamespace' => 'console\controllers',
    'modules' => [
        'gii' => 'yii\gii\Module',
    ],
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'user' => [
            'class' => 'yii\web\User',
            'identityClass' => 'common\models\UserConsole',
            //'enableAutoLogin' => true,
        ],
    ],
    'params' => $params,
];

目录console\controllers为空。

2 个答案:

答案 0 :(得分:2)

控制台应用程序没有user组件。因此,您需要在user中配置/console/config/main.php组件。

喜欢,

<强> /console/config/main.php

 'components' => [
 .........
 ......
        'user' => [
            'class' => 'yii\web\User',
            'identityClass' => 'app\models\User',
            //'enableAutoLogin' => true,
        ],
 .......
]

答案 1 :(得分:1)

原因很简单,因为用户是一个组件。 Guide说明了应用程序组件。因此,在控制台应用程序&#39;用户&#39; 不存在。因此,您需要在config/main.php.中配置用户组件然后尝试以下代码:

'components'=>array(
    'user'=>array(
        'class' => 'WebUser',
    ),
    'session' => [ // for use session in console application
        'class' => 'yii\web\Session'
    ],

'components'=>array(
    'user'=>array(
        'class' => 'yii\web\User',
        'identityClass' => 'app\models\User',
    ),
    'session' => [ // for use session in console application
        'class' => 'yii\web\Session'
    ],

user:表示用户认证信息。此组件仅在Web应用程序中可用。有关详细信息,请参阅“身份验证”部分。