经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
YII分模块加载路由的实现方法
来源:jb51  时间:2018/10/8 8:45:30  对本文有异议

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

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

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

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

  1. 'modules' => [
  2. 'setup' => [
  3. 'class' => 'appcomponents\modules\setup\Module',
  4. ],
  5. 'shareorder' => [
  6. 'class' => 'appcomponents\modules\shareorder\Module',
  7. ],
  8. ]

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

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

modules.php里面配置如下

  1. return [
  2. 'setup' => [
  3. 'class' => 'appcomponents\modules\setup\Module',
  4. ],
  5. 'shareorder' => [
  6. 'class' => 'appcomponents\modules\shareorder\Module',
  7. ],
  8. ];

然后修改rules.php

  1. $default = [
  2.  
  3.  
  4. ];
  5. $modules = require __DIR__.'./modules.php';
  6. $roles = [];
  7. foreach ($modules as $module)
  8. {
  9. $class = new ReflectionClass($module['class']);
  10. $filePath = $class->getFileName();
  11. $filePath = str_replace('Module','rules',$filePath);
  12. if(file_exists($filePath))
  13. {
  14. $role = require $filePath;
  15. $roles = array_merge($roles,$role);
  16. }
  17. }
  18. return array_merge($roles,$default);。

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

每个模块的目录结构

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号