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.