YII分模塊加載路由的實現(xiàn)方法
起因。因為項目比較大了之后劃了很多模塊。就使得config下面的路由文件變得很龐大,變得不好維護。這個時候就想如果可以把路由拆分到不同模塊去自己管理,就會變得清晰很多。
拆了之后項目配置結(jié)構(gòu)如下
新增了一個modules.php
來管理模塊的加載
調(diào)整之前 web.php
的模塊加載配置如下
'modules' => [ 'setup' => [ 'class' => 'appcomponents\modules\setup\Module', ], 'shareorder' => [ 'class' => 'appcomponents\modules\shareorder\Module', ], ]
調(diào)整之后 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 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
文件
每個模塊的目錄結(jié)構(gòu)
其中Modules.php
是配置當前模塊,加載命名空間等。rules.php
為當前模塊的下的路由配置
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Yii2創(chuàng)建控制器(createController)方法詳解
這篇文章主要介紹了Yii2創(chuàng)建控制器(createController)的方法,結(jié)合實例形式分析了Yii創(chuàng)建控制器所使用到的方法、操作步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07php使用goto實現(xiàn)自動重啟swoole、reactphp、workerman服務(wù)的代碼
這篇文章主要介紹了php使用goto實現(xiàn)自動重啟swoole、reactphp、workerman服務(wù)的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04PHP錯誤Warning: Cannot modify header information - headers alr
這篇文章主要介紹了PHP錯誤Warning: Cannot modify header information - headers already sent by解決方法,需要的朋友可以參考下2014-09-09php操作MongoDB基礎(chǔ)教程(連接、新增、修改、刪除、查詢)
這篇文章主要介紹了php操作MongoDB簡明教程,包括連接、新增、修改、刪除、查詢等,需要的朋友可以參考下2014-03-03