YII分模块加载路由

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了YII分模块加载路由脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
起因。因为项目比较大了之后划了很多模块。就使得config下面的路由文件变得很庞大,变得不好维护。这个时候就想如果可以把路由拆分到不同模块去自己管理,就会变得清晰很多。
拆了之后项目配置结构如下

clipboard.png

新增了一个modules.php来管理模块的加载

调整之前 web.php的模块加载配置如下

'modules' => [
    'setup' => [
        'class' => 'appcomponentsmodulessetupModule',
    ],
    'shareorder' => [
        'class' => 'appcomponentsmodulesshareorderModule',
    ],
]

调整之后 web.php模块配置如下
'modules' => require (__DIR__).'/modules.php',
modules.php里面配置如下

return [
    'setup' => [
        'class' => 'appcomponentsmodulessetupModule',
    ],
    'shareorder' => [
        'class' => 'appcomponentsmodulesshareorderModule',
    ],
];

然后修改rules.php

$default = [


];
$modules = require __DIR__.'./modules.php';
$roles = [];
foreach ($modules as $module)
{
    $class = new ReflectionClass($module['class']);
    $filePath = $class->getFileName();
    $filePath = str_replace('Module','rules',$filePath);
    if(file_exists($filePath))
    {
        $role = require $filePath;
        $roles = array_merge($roles,$role);
    }
}
return array_merge($roles,$default);。

利用反射找到每个模块的真实路径,然后加载当前模块下的rules.php文件

每个模块的目录结构

clipboard.png

其中Modules.php是配置当前模块,加载命名空间等。rules.php为当前模块的下的路由配置

原文地址

脚本宝典总结

以上是脚本宝典为你收集整理的YII分模块加载路由全部内容,希望文章能够帮你解决YII分模块加载路由所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: