Yii2 如何在模块中创建控制台命令?

Yii2 如何在模块中创建控制台命令?,yii2,Yii2,控制台命令,如/yii hello/world 我正在使用yii应用程序basic 我想要的不是在dircommands/中创建控制台命令,而是在模块中创建控制台命令 这是一个很好的例子 遵循以下教程步骤: 1) Create a new module in your application. 2) Edit the Module.php. 3) Create your folder and command inside your module. 4) Add your module to

控制台命令
,如
/yii hello/world

我正在使用
yii应用程序basic

我想要的不是在dir
commands/
中创建控制台命令,而是在
模块中创建控制台命令

这是一个很好的例子

遵循以下教程步骤:

1) Create a new module in your application. 
2) Edit the Module.php. 
3) Create your folder and command inside your module. 
4) Add your module to app configurations.
1)您的模块应实现BootstrapInterface

class Module extends \yii\base\Module implements \yii\base\BootstrapInterface
{

    public function bootstrap($app)
    {
        if ($app instanceof \yii\console\Application) {
            $this->controllerNamespace = 'app\modules\my_module\commands';
        }
    }

}
namespace app\modules\my_module\commands;

class ConsoleController extends \yii\console\Controller
{
    public function actionIndex()
    {
        echo "Hello World\n";
    }
}
'bootstrap' => [
    // ... other bootstrap components ...
    'my_module',
],
'modules' => [
    // ... other modules ...
    'my_module' => [
        'class' => 'app\modules\my_module\Module',
    ],
],
yii my_module/console/index
2)在模块
命令
文件夹中创建控制台控制器

class Module extends \yii\base\Module implements \yii\base\BootstrapInterface
{

    public function bootstrap($app)
    {
        if ($app instanceof \yii\console\Application) {
            $this->controllerNamespace = 'app\modules\my_module\commands';
        }
    }

}
namespace app\modules\my_module\commands;

class ConsoleController extends \yii\console\Controller
{
    public function actionIndex()
    {
        echo "Hello World\n";
    }
}
'bootstrap' => [
    // ... other bootstrap components ...
    'my_module',
],
'modules' => [
    // ... other modules ...
    'my_module' => [
        'class' => 'app\modules\my_module\Module',
    ],
],
yii my_module/console/index
3)将模块添加到应用程序控制台配置
config/console.php

class Module extends \yii\base\Module implements \yii\base\BootstrapInterface
{

    public function bootstrap($app)
    {
        if ($app instanceof \yii\console\Application) {
            $this->controllerNamespace = 'app\modules\my_module\commands';
        }
    }

}
namespace app\modules\my_module\commands;

class ConsoleController extends \yii\console\Controller
{
    public function actionIndex()
    {
        echo "Hello World\n";
    }
}
'bootstrap' => [
    // ... other bootstrap components ...
    'my_module',
],
'modules' => [
    // ... other modules ...
    'my_module' => [
        'class' => 'app\modules\my_module\Module',
    ],
],
yii my_module/console/index
4)您现在可以使用命令了

class Module extends \yii\base\Module implements \yii\base\BootstrapInterface
{

    public function bootstrap($app)
    {
        if ($app instanceof \yii\console\Application) {
            $this->controllerNamespace = 'app\modules\my_module\commands';
        }
    }

}
namespace app\modules\my_module\commands;

class ConsoleController extends \yii\console\Controller
{
    public function actionIndex()
    {
        echo "Hello World\n";
    }
}
'bootstrap' => [
    // ... other bootstrap components ...
    'my_module',
],
'modules' => [
    // ... other modules ...
    'my_module' => [
        'class' => 'app\modules\my_module\Module',
    ],
],
yii my_module/console/index

我用分机试过了,但没用。知道为什么吗?如果我希望我的模块命令具有以下格式:
yii my_module/command
(不带控制器名称),该怎么办?