How make a console command in a new module

I want to make a module to put on Github.

I created a new Module in Gii, but I dont have idea how to create a new command.

I created a directory ‘commands’ and two tasks. I added a Module class in console/config/main.php, but when i run the command ‘php yii’ my tasks doesn’t appear in list.

How can I do to put my tasks in console?

Thanks for this!

Ok, they didn’t appear as a command tips, but did they work if you run ‘yii module/controller’?

I’ve had similar problem here http://www.yiiframework.com/forum/index.php/topic/64372-command-line-from-module-double-name

Solution is to set proper controllerNamespace for console app.

Hi!

I just tried the same thing like Renato Cassino but was also not able to get it running.

Thanks to Bizley that was very helpful!

Here is a small guide how to get console commands running inside modules / extensions.

For this I used a fresh yii2 basic application template and I only describe things that are really needed to get the command running.

1) Create a new module in your application.

(I named it "example_commands" for this instructions)




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();


        // custom initialization code goes here

    }


    public function bootstrap($app)

    {

        if ($app instanceof \yii\console\Application) {


        	$this->controllerNamespace = 'app\modules\example_commands\commands';


        }

    }

}



3) Create your folder and command 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' => [

        'log', 

        'gii', 

        'example_commands'

    ],

    'modules' => [

        'gii' => 'yii\gii\Module',

        'example_commands' => [

            'class' => 'app\modules\example_commands\Module',

        ],

    ],



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

5) That was it - it should work now.




> yii 


... ... ... 

- example_commands/testing                    This command echoes the first argument that you have entered.

    example_commands/testing/index (default)  This command echoes what you have entered as the message.

... ... ...



Regards and thanks again to Bizley.

I’m glad I could help ;)

Anyway, one thing I noticed. I think it’s better to set it like that:


public function bootstrap($app)

{

    if ($app instanceof \yii\console\Application) {


        $this->controllerNamespace = 'app\modules\example_commands\commands';


    }

}

It’s because you can not relay on the name of module (‘example_commands’ in $app->getModule(‘example_commands’)) because user is free to name it whatever he wants.

Copied this from your other thread and have not thought about it. :)

But Yes, you are absolutley correct.

$this->controllerNamespace is way better.

I will edit my previous post regarding this change. ;)

Thanks again.

Regards

Thanks for this, men!

I forgot to add in my config/console.php the bootstrap.

Thanks for this xD

Just as an update here for others that may run across this post. I was not able to get command line to work until I added single quotes around the class.


./yii gii/module --moduleID=example_commands --moduleClass=app\modules\example_commands\Module

Running 'Module Generator'...


Code not generated. Please fix the following errors:


 - moduleClass: Module class must be properly namespaced.

The following worked. This was with the yii basic template pulled today 09/08/15


./yii gii/module --moduleID=example_commands --moduleClass='app\modules\example_commands\Module'