Skip to content

Commit 71b83ac

Browse files
committed
路由注册增加sub方法 用于注册子目录分组
1 parent aca4f9f commit 71b83ac

4 files changed

Lines changed: 37 additions & 86 deletions

File tree

src/think/Http.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212

1313
namespace think;
1414

15-
use DirectoryIterator;
1615
use think\event\HttpEnd;
1716
use think\event\HttpRun;
1817
use think\event\RouteLoaded;
1918
use think\exception\Handle;
20-
use think\facade\Route;
2119
use Throwable;
2220

2321
/**
@@ -231,28 +229,9 @@ protected function loadRoutes(): void
231229
$routePath = $this->getRoutePath();
232230

233231
if (is_dir($routePath)) {
234-
$iterator = new DirectoryIterator($routePath);
235-
$groupName = [];
236-
foreach ($iterator as $fileinfo) {
237-
if ($fileinfo->isDot()) {
238-
continue;
239-
}
240-
241-
if ($fileinfo->getType() == 'file' && $fileinfo->getExtension() == 'php') {
242-
// 加载路由定义文件
243-
include_once $fileinfo->getRealPath();
244-
}
245-
246-
if ($fileinfo->isDir()) {
247-
// 自动为子目录注册分组路由
248-
$groupName[] = str_replace('\\', '/', substr_replace($fileinfo->getPathname(), '', 0, strlen($routePath)));
249-
}
250-
}
251-
252-
foreach ($groupName as $group) {
253-
if (!Route::getRuleName()->hasGroup($group)) {
254-
Route::group($group);
255-
}
232+
$files = glob($routePath . '*.php');
233+
foreach ($files as $file) {
234+
include $file;
256235
}
257236
}
258237

src/think/Route.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,20 @@ public function setCrossDomainRule(Rule $rule)
459459
return $this;
460460
}
461461

462+
/**
463+
* 注册子目录路由分组
464+
* @access public
465+
* @param string $name 分组名称或者参数
466+
* @param string $subdir 分组子目录名(默认和分组名同名)
467+
* @return RuleGroup
468+
*/
469+
public function sub(string $group, string $subdir = ''): RuleGroup
470+
{
471+
return (new RuleGroup($this, $this->group, $group, null, $this->lazy, $subdir ?: $group))
472+
->removeSlash($this->removeSlash)
473+
->mergeRuleRegex($this->mergeRuleRegex);
474+
}
475+
462476
/**
463477
* 注册路由分组
464478
* @access public

src/think/route/RuleGroup.php

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/think/route/RuleName.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,6 @@ public function getGroup(string $name): ?RuleGroup
106106
return $this->group[strtolower($name)] ?? null;
107107
}
108108

109-
/**
110-
* 是否已经存在分组
111-
* @access public
112-
* @param string $name 路由分组标识
113-
* @return bool
114-
*/
115-
public function hasGroup(string $name): bool
116-
{
117-
return isset($this->group[strtolower($name)]);
118-
}
119-
120109
/**
121110
* 清空路由规则
122111
* @access public

0 commit comments

Comments
 (0)