我对Yii2很新。我创建了一个前端Yii2应用程序,现在我想定义一个每晚都会调用一些动作的cronjob。
我为此创建了一个简单的控制台应用程序,按照此处的教程:https://www.yiiframework.com/wiki/646/how-to-implement-cron-in-yii-2,使用以下代码:
namespace console\controllers;
use yii\console\Controller;
class CronController extends Controller {
public function actionIndex() {
echo "cron service runnning";
}
}
我尝试通过运行它来调用它:
php yii cron/index
但是我收到以下错误:
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}
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'
]
]
];
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,
];
我不知道会出现什么问题。
操作系统:CentOS 7,PHP版本7
答案 0 :(得分:2)
//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' => ['@'], ], ], ],
这部分配置是个问题。 AccessControl
在控制台应用程序配置中没有任何意义(控制台应用程序没有Yii:$app->user
)。由于您在公共配置中进行了设置,因此它也可用于控制台应用程序的配置。
您应该将此设置移至更高级别,可能移至frontend/config/main.php
和backend/config/main.php
,因此它仅用于Web应用程序。