Yii 2.0: YII2: Create console commands inside a module or extension

5 followers

Here is a small guide how to get console commands running inside modules & extensions. For this guide I used a fresh yii2 basic application template.

1) Create a new module in your application.
(I named it "example_commands" for this instructions)

Generate new module from commandline with gii (or use gii-webinterface)

command> yii gii/module --moduleID=example_commands --moduleClass=app\modules\example_commands\Module
 
Running 'Module Generator'...
 
The following files will be generated:
        [new] modules\example_commands.php
        [new] modules\controllers\DefaultController.php
        [new] modules\views\default\index.php
 
Ready to generate the selected files? (yes|no) [yes]:y

2) Edit the Module.php
app/modules/example_commands/Module.php

namespace app\modules\example_commands;
 
use Yii;
use yii\base\BootstrapInterface;
use yii\base\Module as BaseModule;
 
class Module extends BaseModule implements BootstrapInterface
{
    public $controllerNamespace = 'app\modules\example_commands\controllers';
 
    public function init()
    {
        parent::init();
    }
 
    public function bootstrap($app)
    {
        if ($app instanceof \yii\console\Application) {
            $this->controllerNamespace = 'app\modules\example_commands\commands';
        }
    }
}
[/code]

3) Create your folder & command class inside your module:
app/modules/example_commands/commands/TestingController.php

namespace app\modules\example_commands\commands;
 
use yii\console\Controller;
use yii\helpers\Console;
 
class TestingController extends Controller
{
    public function actionIndex($message = 'hello world from module')
    {
        echo $message . "\n";
    }
}

4) Add your module to app configurations:
app/config/console.php

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

Don't forget app/config/web.php if you have non-console stuff in your module / extension.

5) That was it - you can use your command now like:
moduleId/controller/action

command>yii example_commands/testing/index
hello world from module

Total 5 comments

#19888 report it
MetaCrawler at 2016/06/21 02:59pm
Reverted to "original" version.

Hey guys,

Thanks for your input. My original article had it described like mentioned by "mithun mandal", but it was edited by someone and I missed it.

I have now restored the "original" and correct revision of the article. Best Regards

#19886 report it
beowulfenator at 2016/06/21 02:33pm
Thanks!

The original method did not work for me, but then I tried mithun mandal's method:

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

And it worked!

#19795 report it
mithun mandal at 2016/03/04 02:27pm
can be done in extension as well check this url below

Please check this piece of code.

https://github.com/yiisoft/yii2-mongodb/blob/master/console/controllers/MigrateController.php

#19794 report it
jozek; at 2016/03/03 09:17am
Doesn't work in extension

Have you succesfully tried this in extension instead of module? I had to make it by moving code from bootstrap method to init.

#19646 report it
mithun mandal at 2015/10/31 11:59am
can be achieve same with multiple commands
public function bootstrap($app)
    {
        if ($app instanceof \yii\console\Application) {
            $app->controllerMap[$this->id] = [
                'class' => 'app\modules\example_commands\commands\TestingController',
                'module' => $this,
            ];
        }
    }

this can be rewrite with more easier solution. like this shown in below..

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

Leave a comment

Please to leave your comment.

Write new article