• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

YII分模块加载路由的实现方法

php 搞代码 4年前 (2022-01-01) 35次浏览 已收录 0个评论

这篇文章主要介绍了YII分模块加载路由的实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

起因。因为项目比较大了之后划了很多模块。就使得config下面的路由文件变得很庞大,变得不好维护。这个时候就想如果可以把路由拆分到不同模块去自己管理,就会变得清晰很多。

拆了之后项目配置结构如下

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

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

 'modules' => [ 'setup' => [ 'class' => 'appcomponents\modules\setup\Module', ], 'shareorder' => [ 'class' => 'appcomponents\modules\shareorder\Module', ], ]

调整之后 web.php模块配置如下

'modules' => require (__DIR__).'/modules.php',

modules.php里面配置如下

 return [ 'setup' => [ 'class' => 'appcomponents\modules\setup\Module', ], 'shareorder' => [ 'class' => 'appcomponents\modules\shareorder\Module', ], ];

然后修改rules.php

 $default = [ ]; $modules = require __DIR__.'./modules.php'; $roles = []; foreach ($modules as $module) { $class = new Refl<span style="color:transparent">来源gaodai#ma#com搞*代#码网</span>ectionClass($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文件

每个模块的目录结构

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

以上就是YII分模块加载路由的实现方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:YII分模块加载路由的实现方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址