在运行多个应用程序时,在Yii2中使用自定义控制台命令时遇到问题

时间:2015-09-21 15:26:27

标签: php yii yii2 console-application

我正在运行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

我刚刚尝试运行我的第一个自定义控制台命令,但我一直收到错误:

  

错误:未知命令"数据/安装时区"。

我的控制器:

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,
];

知道问题是什么吗?

1 个答案:

答案 0 :(得分:3)

我认为你刚使用了错误的控制器:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="breadcrumbs">
  <span typeof="v:Breadcrumb">
    <a title="Go to Innomations." href="" class="home">Home</a></span>
  >>
  <span typeof="v:Breadcrumb" class="turnoffmain">
    <a rel="v:url" property="v:title" title="Go to Products." href="">Products</a></span> 
</div>

尝试使用此命令: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确实没有定义。

某种深夜节目? ; - )