如何在模块中创建控制台命令? [英] how to create console command in a module?

查看:30
本文介绍了如何在模块中创建控制台命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在使用 yii-app-basic.

我想要的不是在目录 commands/ 中创建控制台命令,而是在 module 中.

what I want is not create console command in the dir commands/ but in a module.

推荐答案

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

}

2) 在您的模块 commands 文件夹 中创建您的控制台控制器:

2) Create your console controller in your module commands folder :

namespace app\modules\my_module\commands;

class ConsoleController extends \yii\console\Controller
{
    public function actionIndex()
    {
        echo "Hello World\n";
    }
}

3) 将您的模块添加到您的应用控制台配置 config/console.php :

3) Add your module to your app console configuration config/console.php :

'bootstrap' => [
    // ... other bootstrap components ...
    'my_module',
],
'modules' => [
    // ... other modules ...
    'my_module' => [
        'class' => 'app\modules\my_module\Module',
    ],
],

4) 您现在可以使用您的命令 :

yii my_module/console/index

这篇关于如何在模块中创建控制台命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆