I created a custom module with console command yii gii/module
I get a double entry with console command yii help
screenshot with double entry for module
this is the directory tree
Module.php
<?php
namespace app\modules\vdreports;
use Yii;
use yii\base\BootstrapInterface;
class Module extends \yii\base\Module implements BootstrapInterface
{
public function bootstrap($app){
// print_r('ik ben in bootstrap'.PHP_EOL);
// var_dump($app->controllerMap);
foreach ($this->coreCommands() as $id => $command) {
if (!isset(Yii::$app->controllerMap[$id])) {
Yii::$app->controllerMap[$id] = $command;
}
}
}
public function init()
{
parent::init();
// custom initialization code goes here
}
public function coreCommands() {
return [
'vdreports' => 'app\modules\vdreports\controllers\DefaultController',
];
}
}
I patched it like this
app/vendor/yiisoft/yii2/console/controllers/HelpController.php
protected function validateControllerClass($controllerClass)
{
if (class_exists($controllerClass)) {
$class = new \ReflectionClass($controllerClass);
// var_dump($class->isAbstract());
// var_dump($class->isSubclassOf('yii\console\Controller'));
// return !$class->isAbstract() && $class->isSubclassOf('yii\console\Controller');
return !$class->isAbstract() && $class->isSubclassOf('yii\console\Controller')
&& !strstr($controllerClass,'vdreports'); // 04/08/2024 Marnik : workaround to fix 'yii help'
}
return false;
}
Now I get a single entry for my module with console command yii help
as desired.
This is definitely not the right way. I don't want to mess up a source file of the yii installation.
Can anybody help?
thanks
Marnik
The double entry is caused by explicitly adding modules's DefaultController
to Yii::$app->controllerMap
.
If you add the module in your console app config (config/console.php
in basic app template, console/config/main.php
in advanced app template) it will automatically register all controllers in Module::$controllerNamespace
property that extends yii\console\Controller
. So you don't need to explicitly add those to Application::$controllerMap
property. This is what created vdreports/default
entry in your yii help
output.
Doing Yii::$app->controllerMap['vdreports'] = 'app\modules\vdreports\controllers\DefaultController';
in your bootstrap()
method is what created vdreports
entry duplicating already existing vdreports/default
entry.
You can simply remove the unnecessary bootstraping to get rid of duplicate entry.
Another option is to move your DefaultController
from app\modules\vdreports\controllers
namespace to another namespace (and folder) to prevent automatic registration and keep the registration during bootstrap. You can put it for example in app\modules\vdreports\commands
namespace. This can be useful if you want to simplify the command.