How to create a console command in a module?

console command e.g. ./yii hello/world .

I am using yii-app-basic .

what I want is not creating a console command in the commands/ directory, but in module .

+6
source share
2 answers

1) BootstrapInterface must be installed in your module :

 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 a console controller in the 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 the console configuration of the config/console.php application :

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

4) Now you can use your command :

 yii my_module/console/index 
+7
source

Here is a good Tutorial and Discussion .

Follow the instructions below:

 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. 
+4
source

All Articles