@@ -51,6 +51,12 @@ class RuleGroup extends Rule
5151 */
5252 protected $ alias ;
5353
54+ /**
55+ * 分组子目录
56+ * @var string
57+ */
58+ protected $ sub ;
59+
5460 /**
5561 * 分组绑定
5662 * @var string
@@ -71,13 +77,15 @@ class RuleGroup extends Rule
7177 * @param string $name 分组名称
7278 * @param mixed $rule 分组路由
7379 * @param bool $lazy 延迟解析
80+ * @param string $sub 分组子目录
7481 */
75- public function __construct (Route $ router , ?RuleGroup $ parent = null , string $ name = '' , $ rule = null , bool $ lazy = false )
82+ public function __construct (Route $ router , ?RuleGroup $ parent = null , string $ name = '' , $ rule = null , bool $ lazy = false , string $ sub = '' )
7683 {
7784 $ this ->router = $ router ;
7885 $ this ->parent = $ parent ;
7986 $ this ->rule = $ rule ;
8087 $ this ->name = trim ($ name , '/ ' );
88+ $ this ->sub = $ sub ;
8189
8290 $ this ->setFullName ();
8391
@@ -104,6 +112,9 @@ protected function setFullName(): void
104112
105113 if ($ this ->parent && $ this ->parent ->getFullName ()) {
106114 $ this ->fullName = $ this ->parent ->getFullName () . ($ this ->name ? '/ ' . $ this ->name : '' );
115+ if ($ this ->sub ) {
116+ $ this ->sub = $ this ->parent ->getFullName () . '/ ' . $ this ->sub ;
117+ }
107118 } else {
108119 $ this ->fullName = $ this ->name ;
109120 }
@@ -136,56 +147,20 @@ public function getAlias(): string
136147 /**
137148 * 自动加载分组路由
138149 * @access protected
150+ * @param string $dir 目录名
139151 * @return void
140152 */
141- protected function loadRoutes (): void
153+ protected function loadRoutes (string $ dir ): void
142154 {
143- $ routePath = root_path ('route ' . DIRECTORY_SEPARATOR . $ this -> fullName );
155+ $ routePath = root_path ('route ' . DIRECTORY_SEPARATOR . $ dir . DIRECTORY_SEPARATOR );
144156 if (is_dir ($ routePath )) {
145- $ origin = $ this ->router ->getGroup ();
146- $ this ->router ->setGroup ($ this );
147- $ iterator = new DirectoryIterator ($ routePath );
148- $ groups = [];
149- foreach ($ iterator as $ fileinfo ) {
150- if ($ fileinfo ->isDot ()) {
151- continue ;
152- }
153-
154- if ($ fileinfo ->getType () == 'file ' && $ fileinfo ->getExtension () == 'php ' ) {
155- // 加载目录下的路由定义文件
156- $ groupName = str_replace ('\\' , '/ ' , substr_replace ($ fileinfo ->getPath (), '' , 0 , strlen ($ routePath )));
157- if ($ groupName ) {
158- $ group = $ this ->router ->getRuleName ->getGroup ($ groupName );
159- $ this ->router ->setGroup ($ group );
160- include_once $ fileinfo ->getRealPath ();
161- $ this ->router ->setGroup ($ this );
162- } else {
163- include_once $ fileinfo ->getRealPath ();
164- }
165- }
166-
167- if ($ fileinfo ->isDir ()) {
168- // 子目录自动作为分组名
169- $ groups [] = str_replace ('\\' , '/ ' , substr_replace ($ fileinfo ->getPathname (), '' , 0 , strlen ($ routePath )));
170- }
157+ $ files = glob ($ routePath . '*.php ' );
158+ foreach ($ files as $ file ) {
159+ include_once $ file ;
171160 }
172-
173- foreach ($ groups as $ group ) {
174- // 自动注册路由分组
175- if (!$ this ->hasGroup ($ group )) {
176- $ this ->router ->group ($ group );
177- }
178- }
179-
180- $ this ->router ->setGroup ($ origin );
181161 }
182162 }
183163
184- protected function hasGroup (string $ name ): bool
185- {
186- return $ this ->router ->getRuleName ()->hasGroup ($ name );
187- }
188-
189164 /**
190165 * 检测分组路由
191166 * @access public
@@ -196,14 +171,6 @@ protected function hasGroup(string $name): bool
196171 */
197172 public function check (Request $ request , string $ url , bool $ completeMatch = false )
198173 {
199- if ($ this ->fullName ) {
200- $ groupName = str_replace ('/ ' , '| ' , $ this ->fullName );
201- if ($ groupName == $ url || 0 === strpos ($ url , $ groupName . '| ' )){
202- // 自动加载分组路由(子目录)
203- $ this ->loadRoutes ();
204- }
205- }
206-
207174 // 检查分组有效性
208175 if (!$ this ->checkOption ($ this ->option , $ request ) || !$ this ->checkUrl ($ url )) {
209176 return false ;
@@ -320,6 +287,8 @@ public function parseGroupRule($rule): void
320287 Container::getInstance ()->invokeFunction ($ rule );
321288 } elseif (is_string ($ rule ) && $ rule ) {
322289 $ this ->bind ($ rule );
290+ } elseif ($ this ->sub ) {
291+ $ this ->loadRoutes ($ this ->sub );
323292 }
324293
325294 $ this ->router ->setGroup ($ origin );
0 commit comments