在 运行 多个应用程序时,让自定义控制台命令在 Yii2 中工作时出现问题
Having issue getting custom console commands to work in Yii2 whilst running multiple applications
我 运行宁 Yii2
并且有几个不同的应用程序设置如下目录结构...
admin/
system/
controllers/
models/
views/
index.php
common/
vendor/
.bowerrc
composer.json
composer.lock
console/
config/
controllers/
yii
yii.bat
css/
images/
js/
system/
controllers/
models/
views/
index.php
我刚开始尝试 运行 我的第一个自定义控制台命令,但我一直收到错误消息:
Error: Unknown command "data/install-timezones".
我的控制器:
namespace console\controllers;
use Yii;
use yii\web\Controller;
use console\models\Timezones;
class DataController extends Controller {
public function actionInsertTimezones() {
echo 'testing';
}
}
我的 console/config/main.php
文件:
Yii::setAlias('@tests', dirname(__DIR__) . '/tests');
$params = require(__DIR__ . '/../../common/config/params.php');
$db = require(__DIR__ . '/../../common/config/db.php');
return [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'console\controllers',
'modules' => [
'gii' => 'yii\gii\Module',
],
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error'],
'logFile' => '@app/runtime/logs/errors.log',
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['warning'],
'logFile' => '@app/runtime/logs/warnings.log',
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['info'],
'logFile' => '@app/runtime/logs/info.log',
'enabled' => false,
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['trace'],
'logFile' => '@app/runtime/logs/trace.log',
'enabled' => false,
],
],
],
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/languages',
],
],
],
'db' => $db,
],
'params' => $params,
];
知道问题出在哪里吗?
我想你只是用错了控制器:
namespace console\controllers;
use Yii;
use yii\console\Controller; // <--- not yii\web\Controller
use console\models\Timezones;
class DataController extends Controller {
public function actionInsertTimezones() {
echo 'testing';
}
}
并尝试使用此命令:yii data/insert-timezone
因为 data/install-timezones
确实未定义。
某种深夜节目? ;-)
我 运行宁 Yii2
并且有几个不同的应用程序设置如下目录结构...
admin/
system/
controllers/
models/
views/
index.php
common/
vendor/
.bowerrc
composer.json
composer.lock
console/
config/
controllers/
yii
yii.bat
css/
images/
js/
system/
controllers/
models/
views/
index.php
我刚开始尝试 运行 我的第一个自定义控制台命令,但我一直收到错误消息:
Error: Unknown command "data/install-timezones".
我的控制器:
namespace console\controllers;
use Yii;
use yii\web\Controller;
use console\models\Timezones;
class DataController extends Controller {
public function actionInsertTimezones() {
echo 'testing';
}
}
我的 console/config/main.php
文件:
Yii::setAlias('@tests', dirname(__DIR__) . '/tests');
$params = require(__DIR__ . '/../../common/config/params.php');
$db = require(__DIR__ . '/../../common/config/db.php');
return [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log', 'gii'],
'controllerNamespace' => 'console\controllers',
'modules' => [
'gii' => 'yii\gii\Module',
],
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error'],
'logFile' => '@app/runtime/logs/errors.log',
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['warning'],
'logFile' => '@app/runtime/logs/warnings.log',
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['info'],
'logFile' => '@app/runtime/logs/info.log',
'enabled' => false,
],
[
'class' => 'yii\log\FileTarget',
'levels' => ['trace'],
'logFile' => '@app/runtime/logs/trace.log',
'enabled' => false,
],
],
],
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@common/languages',
],
],
],
'db' => $db,
],
'params' => $params,
];
知道问题出在哪里吗?
我想你只是用错了控制器:
namespace console\controllers;
use Yii;
use yii\console\Controller; // <--- not yii\web\Controller
use console\models\Timezones;
class DataController extends Controller {
public function actionInsertTimezones() {
echo 'testing';
}
}
并尝试使用此命令:yii data/insert-timezone
因为 data/install-timezones
确实未定义。
某种深夜节目? ;-)