Skip to content

Commit 49804b6

Browse files
committed
改进Route类group方法用法
1 parent 71b83ac commit 49804b6

2 files changed

Lines changed: 16 additions & 31 deletions

File tree

src/think/Route.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -459,20 +459,6 @@ 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-
476462
/**
477463
* 注册路由分组
478464
* @access public

src/think/route/RuleGroup.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,22 @@ class RuleGroup extends Rule
7777
* @param string $name 分组名称
7878
* @param mixed $rule 分组路由
7979
* @param bool $lazy 延迟解析
80-
* @param string $sub 分组子目录
8180
*/
82-
public function __construct(Route $router, ?RuleGroup $parent = null, string $name = '', $rule = null, bool $lazy = false, string $sub = '')
81+
public function __construct(Route $router, ?RuleGroup $parent = null, string $name = '', $rule = null, bool $lazy = false)
8382
{
8483
$this->router = $router;
8584
$this->parent = $parent;
8685
$this->rule = $rule;
8786
$this->name = trim($name, '/');
88-
$this->sub = $sub;
87+
88+
if ($name && is_string($rule) || is_null($rule)) {
89+
if (is_subclass_of($rule, Dispatch::class, false)) {
90+
$this->dispatcher($rule);
91+
$this->rule = '';
92+
} else {
93+
$this->sub = $rule ?: $this->name;
94+
}
95+
}
8996

9097
$this->setFullName();
9198

@@ -95,7 +102,7 @@ public function __construct(Route $router, ?RuleGroup $parent = null, string $na
95102
}
96103

97104
if (!$lazy) {
98-
$this->parseGroupRule($rule);
105+
$this->parseGroupRule();
99106
}
100107
}
101108

@@ -113,7 +120,7 @@ protected function setFullName(): void
113120
if ($this->parent && $this->parent->getFullName()) {
114121
$this->fullName = $this->parent->getFullName() . ($this->name ? '/' . $this->name : '');
115122
if ($this->sub) {
116-
$this->sub = $this->parent->getFullName() . '/' . $this->sub ;
123+
$this->sub = $this->parent->getFullName() . '/' . $this->sub;
117124
}
118125
} else {
119126
$this->fullName = $this->name;
@@ -178,7 +185,7 @@ public function check(Request $request, string $url, bool $completeMatch = false
178185

179186
// 解析分组路由
180187
if (!$this->hasParsed) {
181-
$this->parseGroupRule($this->rule);
188+
$this->parseGroupRule();
182189
}
183190

184191
// 获取当前路由规则
@@ -270,23 +277,15 @@ public function alias(string $alias)
270277
/**
271278
* 解析分组和域名的路由规则及绑定
272279
* @access public
273-
* @param mixed $rule 路由规则
274280
* @return void
275281
*/
276-
public function parseGroupRule($rule): void
282+
public function parseGroupRule(): void
277283
{
278-
if (is_string($rule) && is_subclass_of($rule, Dispatch::class)) {
279-
$this->dispatcher($rule);
280-
return;
281-
}
282-
283284
$origin = $this->router->getGroup();
284285
$this->router->setGroup($this);
285286

286-
if ($rule instanceof Closure) {
287-
Container::getInstance()->invokeFunction($rule);
288-
} elseif (is_string($rule) && $rule) {
289-
$this->bind($rule);
287+
if ($this->rule instanceof Closure) {
288+
Container::getInstance()->invokeFunction($this->rule);
290289
} elseif ($this->sub) {
291290
$this->loadRoutes($this->sub);
292291
}

0 commit comments

Comments
 (0)