how to create console command in a module?

我怕爱的太早我们不能终老 提交于 2019-12-07 04:00:36

问题


console command, like ./yii hello/world.

I'm using yii-app-basic.

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


回答1:


1) Your module should implements 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) 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) 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) You can now use your command :

yii my_module/console/index



回答2:


Here is a good Tutorial and Discussion.

Follow Below Steps on Tutorial:

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.


来源:https://stackoverflow.com/questions/32711006/how-to-create-console-command-in-a-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!