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
source share