Search code examples
phpyii2

Yii2 console app error 'Failed to instantiate component or class "user".'


I'm pretty new to Yii2. I created a frontend Yii2 app and now I want to define a cronjob that would call some action every night.

I created a simple console app for that, following the tutorial from here: https://www.yiiframework.com/wiki/646/how-to-implement-cron-in-yii-2, with the following code:

namespace console\controllers;

use yii\console\Controller;

class CronController extends Controller {
    public function actionIndex() {
        echo "cron service runnning";
    }
}

I try to call it by running this:

php yii cron/index

But I get the following error:

    Exception 'yii\base\InvalidConfigException' with message 'Failed to instantiate component or class "user".'

in /var/www/html/my-yii2-app/vendor/yiisoft/yii2/di/Instance.php:139

Stack trace:
#0 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/filters/AccessControl.php(100): yii\di\Instance::ensure(Object(yii\di\Instance), 'yii\\web\\User')
#1 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/base/Object.php(107): yii\filters\AccessControl->init()
#2 [internal function]: yii\base\Object->__construct(Array)
#3 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/di/Container.php(381): ReflectionClass->newInstanceArgs(Array)
#4 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/di/Container.php(156): yii\di\Container->build('yii\\filters\\Acc...', Array, Array)
#5 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/BaseYii.php(348): yii\di\Container->get('yii\\filters\\Acc...', Array, Array)
#6 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/base/Component.php(183): yii\BaseYii::createObject(Array)
#7 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/BaseYii.php(529): yii\base\Component->__set('beforeRequest', Array)
#8 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/base/Object.php(105): yii\BaseYii::configure(Object(yii\console\Application), Array)
#9 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/base/Application.php(205): yii\base\Object->__construct(Array)
#10 /var/www/html/my-yii2-app/vendor/yiisoft/yii2/console/Application.php(89): yii\base\Application->__construct(Array)
#11 /var/www/html/my-yii2-app/yii(26): yii\console\Application->__construct(Array)
#12 {main}

Content of common/config/main.php:

return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
    ],

    //if guest user access site so, redirect to login page.
    'as beforeRequest' => [  
        'class' => 'yii\filters\AccessControl',
        'rules' => [
            [
                'actions' => ['login', 'error'],
                'allow' => true,
            ],
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ],
    ],
    // end if guest user access site so, redirect to login page.
    'language' => 'ro-RO',

    'modules' => [
        'attachments' => [
            'class' => nemmo\attachments\Module::className(),
            'tempPath' => '@app/../uploads/temp',
            'storePath' => '@app/../uploads/store',
            'rules' => [ // Rules according to the FileValidator
                'maxFiles' => 10, // Allow to upload maximum 3 files, default to 3
                //'mimeTypes' => 'image/png', // Only png images
                //'maxSize' => 1024 * 1024 // 1 MB
            ],
            //'tableName' => '{{%attachments}}' // Optional, default to 'attach_file'
            'tableName' => 'attachment' // Optional, default to 'attach_file'
        ]
    ]
];

Content of console/config/main.php:

$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'],
    'controllerNamespace' => 'console\controllers',
    'controllerMap' => [
        'fixture' => [
            'class' => 'yii\console\controllers\FixtureController',
            'namespace' => 'common\fixtures',
          ],
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationNamespaces' => [
                'nemmo\attachments\migrations',
            ],
        ],
    ],
    'components' => [
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
    ],
    'params' => $params,
];

I have no idea what could be wrong.

OS: CentOS 7, PHP version 7


Solution

  • //if guest user access site so, redirect to login page.
    'as beforeRequest' => [  
        'class' => 'yii\filters\AccessControl',
        'rules' => [
            [
                'actions' => ['login', 'error'],
                'allow' => true,
            ],
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ],
    ],
    

    This part of config is a problem. AccessControl does not have any sense in console application config (there is no Yii:$app->user for console application). Since you're setting it in common config, it is also used in configuration of console application.

    You should move this setting at higher level, probably to frontend/config/main.php and backend/config/main.php, so it will be used only for web applications.