因为。因为很多模块是在项目比较大之后画出来的。这使得配置下的路由文件庞大且难以维护。这个时候,我觉得如果能把路线拆分成不同的模块,自己管理,会清楚很多。
拆除后,项目配置结构如下
添加了一个新的modules.php来管理模块的加载
调整前web.php的模块加载配置如下
模块'=[' setup '=[' class '=' appcomponents modules setup module ',],' share order '=[' class '=' appcomponents modules share order module ',],]调整后,web.php模块的配置如下
模块'=需要(__DIR__)。/modules.php ',
modules.php的配置如下
返回[' setup '=[' class '=' appcomponents modules setup Module ',],' share order '=[' class '=' appcomponents modules share order Module ',],];然后修改rules.php
$ default=[];$modules=需要__DIR__。/modules . PHP ';$ roles=[];foreach($ modules as $ module){ $ class=new ReflectionClass($ module[' class ']);$ filePath=$ class-getFileName();$filePath=str_replace('Module ',' rules ',$ file path);if(file _ exists($ file path)){ $ role=require $ file path;$roles=array_merge($roles,$ role);}}return array_merge($roles,$ default);通过反射找到每个模块的真实路径,然后在当前模块下加载rules.php文件
每个模块的目录结构
Modules.php是配置当前模块,加载命名空间,等等。Rules.php是当前模块下的路由配置
以上就是本文的全部内容。希望对大家的学习有帮助,支持我们。