Brett
Brett

Reputation: 20049

Having issue getting custom console commands to work in Yii2 whilst running multiple applications

I'm running Yii2 and have a few different applications setup with the below directory structure...

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

I just got around to trying to run my first custom console command but I keep getting the error:

Error: Unknown command "data/install-timezones".

My controller:

namespace console\controllers;

use Yii;
use yii\web\Controller;
use console\models\Timezones;

class DataController extends Controller {

    public function actionInsertTimezones() {

        echo 'testing';

    }

}

My console/config/main.php file:

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

Any idea what the issue is?

Upvotes: 2

Views: 1508

Answers (1)

robsch
robsch

Reputation: 9728

I think you just have used the wrong controller:

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';

    }

}

And try it with this command: yii data/insert-timezone since data/install-timezones is really not defined.

Some kind of late-night programming? ;-)

Upvotes: 4

Related Questions