Skip to content

Commit a3d5ca5

Browse files
committed
路由定义支持分组子目录
1 parent 92bba67 commit a3d5ca5

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

src/think/route/RuleGroup.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,27 @@ public function getAlias(): string
132132
return $this->alias ?: '';
133133
}
134134

135+
/**
136+
* 自动加载分组路由
137+
* @access protected
138+
* @return void
139+
*/
140+
protected function loadRoutes(): void
141+
{
142+
// 加载路由定义
143+
$routePath = root_path('route' . DIRECTORY_SEPARATOR . $this->fullName);
144+
145+
if (is_dir($routePath)) {
146+
$origin = $this->router->getGroup();
147+
$this->router->setGroup($this);
148+
$files = glob($routePath . '*.php');
149+
foreach ($files as $file) {
150+
include_once $file;
151+
}
152+
$this->router->setGroup($origin);
153+
}
154+
}
155+
135156
/**
136157
* 检测分组路由
137158
* @access public
@@ -142,6 +163,14 @@ public function getAlias(): string
142163
*/
143164
public function check(Request $request, string $url, bool $completeMatch = false)
144165
{
166+
if ($this->fullName) {
167+
$groupName = str_replace('/', '|', $this->fullName);
168+
if ($groupName == $url || 0 === strpos($url, $groupName . '|')){
169+
// 自动加载分组路由(子目录)
170+
$this->loadRoutes();
171+
}
172+
}
173+
145174
// 检查分组有效性
146175
if (!$this->checkOption($this->option, $request) || !$this->checkUrl($url)) {
147176
return false;

0 commit comments

Comments
 (0)