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
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'; } }
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 toinit
.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
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!
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
As @jozek pointed out the below is enough.
public function init() { parent::init(); if (Yii::$app instanceof \yii\console\Application) { $this->controllerNamespace = 'app\modules\<module_name\commands'; }
app\modules\module_name\commands\HelloController.php
class HelloController extends Controller { public function actionWorld() { echo "Hello world"; } }
console.php (basic template) or console/config/main.php (advanced template)
'modules' => [ // ... other modules ... 'module_name' => [ 'class' => 'app\modules\module_name\Module', ], ],
Execute command from CLI
./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.
Use "..." for moduleClass
./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.
Use "..." for moduleClass
./yii gii/module --moduleID=example_commands --moduleClass="app\modules\example_commands\Module"
2) Edit the Module.php The following code has an error
`
phppublic $controllerNamespace = 'app\modules\example_commands\controllers';
correct
phppublic $controllerNamespace = 'app\modules\example_commands\commands';
`
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.