Is there any solution to add routes from module configuration?
Example. We have main config where we describe
\'components\' => [
\'urlManage
Separating url rules for modules is mentioned in official documentation here.
And I think this is more optimum approach unlike merging and declaring all rules in one config file.
The rules are parsed in order they are declared (this is mentioned here), so with the separation you can skip other modules urls. In case of large amount of rules it can give perfomance boost.
So I did this way (this is full answer for a question).
Create Bootstrap class special for module.
namespace app\extensions;
use yii\base\BootstrapInterface;
/**
* Class ModuleBootstrap
*
* @package app\extensions
*/
class ModuleBootstrap implements BootstrapInterface
{
/**
* @param \yii\base\Application $oApplication
*/
public function bootstrap($oApplication)
{
$aModuleList = $oApplication->getModules();
foreach ($aModuleList as $sKey => $aModule) {
if (is_array($aModule) && strpos($aModule['class'], 'app\modules') === 0) {
$sFilePathConfig = FILE_PATH_ROOT . DS . 'modules' . DS . $sKey . DS . 'config' . DS . '_routes.php';
if (file_exists($sFilePathConfig)) {
$oApplication->getUrlManager()->addRules(require($sFilePathConfig));
}
}
}
}
}
Create route file in module folder (/modules/XXX/config/_routes.php)
return [
'/sales' => '/sales/index/index',
'/sales/company' => '/sales/company/index',
'/sales/company/view/<sID:\d+>' => '/sales/company/view',
'/sales/company/update/<sID:\d+>' => '/sales/company/update',
'/sales/company/delete/<sID:\d+>' => '/sales/company/delete',
];
Add boostrap to main config file
$aConfig = [
// some code
'bootstrap' => [
// some code
'app\extensions\ModuleBootstrap',
],
'modules' => [
// some code
'sales' => ['class' => 'app\modules\sales\SalesModule']
]
]
return $aConfig;
That's it. We can define routes only in module 'route' config.
PS: I don't like detection if (is_array($aModule) && strpos($aModule['class'], 'app\modules') === 0)
(I mean NOT 'debug', 'log', 'gii' or other native Yii2 modules) maybe someone can suggest better solution?
And this will be more clean and reliable. I have found this on Yii2's github repo here.
<?php
namespace backend\modules\webmasters;
use Yii;
use yii\base\BootstrapInterface;
class Module extends \yii\base\Module implements BootstrapInterface
{
public $controllerNamespace = 'backend\modules\webmasters\controllers';
public function init()
{
parent::init();
// initialize the module with the configuration loaded from config.php
Yii::configure($this, require(__DIR__ . '/config/main.php'));
}
/**
* @inheritdoc
*/
public function bootstrap($app)
{
$app->getUrlManager()->addRules([
[
'class' => 'yii\web\UrlRule',
'pattern' => $this->id,
'route' => $this->id . '/tools/index'
],
[
'class' => 'yii\web\UrlRule',
'pattern' => $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>',
'route' => $this->id . '/<controller>/<action>'
],
], false);
}
}
and just configure your main.php config file's bootstrap section as
'bootstrap' => [
'log',
'webmasters'
]
'modules' => [
'webmasters' => [
'class' => 'backend\modules\webmasters\Module'
]
]