Skip to content

Commit a35a1b7

Browse files
committed
update readme. some modify for Router
1 parent 7248513 commit a35a1b7

File tree

4 files changed

+188
-173
lines changed

4 files changed

+188
-173
lines changed

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,11 @@ $router->group('/user', function () {
179179
});
180180

181181
// 使用 控制器
182-
$router->get('/', app\controllers\HomeController::class);
183-
$router->get('/index', 'app\controllers\HomeController@index');
182+
$router->get('/', App\Controllers\HomeController::class);
183+
$router->get('/index', 'App\Controllers\HomeController@index');
184+
185+
// 使用 rest() 可以快速将一个控制器类注册成一组 RESTful 路由
186+
$router->rest('/users', App\Controllers\UserController::class);
184187

185188
// 可以注册一个备用路由处理。 当没匹配到时,就会使用它
186189
$router->any('*', 'fallback_handler');
@@ -403,10 +406,10 @@ $dispatcher->on('notFound', function ($uri) {
403406
通过`@`符号连接控制器类和方法名可以指定执行方法。
404407

405408
```php
406-
$router->get('/', app\controllers\HomeController::class);
409+
$router->get('/', App\Controllers\HomeController::class);
407410

408-
$router->get('/index', 'app\controllers\HomeController@index');
409-
$router->get('/about', 'app\controllers\HomeController@about');
411+
$router->get('/index', 'App\Controllers\HomeController@index');
412+
$router->get('/about', 'App\Controllers\HomeController@about');
410413
```
411414

412415
> NOTICE: 若第二个参数仅仅是个 类,将会尝试执行通过 `defaultAction` 配置的默认方法
@@ -418,11 +421,11 @@ $router->get('/about', 'app\controllers\HomeController@about');
418421
> NOTICE: 使用动态匹配控制器方法, 应当使用 `any()` 添加路由. 即此时无法限定请求方法 `REQUEST_METHOD`
419422
420423
```php
421-
// 访问 '/home/test' 将会执行 'app\controllers\HomeController::test()'
422-
$router->any('/home/{any}', app\controllers\HomeController::class);
424+
// 访问 '/home/test' 将会执行 'App\Controllers\HomeController::test()'
425+
$router->any('/home/{any}', App\Controllers\HomeController::class);
423426

424427
// 可匹配 '/home', '/home/test' 等
425-
$router->any('/home[/{name}]', app\controllers\HomeController::class);
428+
$router->any('/home[/{name}]', App\Controllers\HomeController::class);
426429
```
427430

428431
> NOTICE: 上面两个的区别是 第一个无法匹配 `/home`
@@ -437,16 +440,16 @@ $router->any('/home[/{name}]', app\controllers\HomeController::class);
437440
示例:
438441

439442
```php
440-
// 访问 '/user', 将会调用 app\controllers\UserController::run('')
441-
$router->get('/user', 'app\controllers\UserController');
443+
// 访问 '/user', 将会调用 App\Controllers\UserController::run('')
444+
$router->get('/user', 'App\Controllers\UserController');
442445

443-
// 访问 '/user/profile', 将会调用 app\controllers\UserController::run('profile')
444-
$router->get('/user/profile', 'app\controllers\UserController');
446+
// 访问 '/user/profile', 将会调用 App\Controllers\UserController::run('profile')
447+
$router->get('/user/profile', 'App\Controllers\UserController');
445448

446449
// 同时配置 'actionExecutor' => 'run' 和 'dynamicAction' => true,
447-
// 访问 '/user', 将会调用 app\controllers\UserController::run('')
448-
// 访问 '/user/profile', 将会调用 app\controllers\UserController::run('profile')
449-
$router->any('/user[/{name}]', 'app\controllers\UserController');
450+
// 访问 '/user', 将会调用 App\Controllers\UserController::run('')
451+
// 访问 '/user/profile', 将会调用 App\Controllers\UserController::run('profile')
452+
$router->any('/user[/{name}]', 'App\Controllers\UserController');
450453
```
451454

452455
## 开始路由匹配和调度

src/AbstractRouter.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,108 @@ abstract class AbstractRouter implements RouterInterface
7676
'controllerSuffix' => '', // eg: 'Controller'
7777
];
7878

79+
/**
80+
* static Routes - no dynamic argument match
81+
* 整个路由 path 都是静态字符串 e.g. '/user/login'
82+
* @var array[]
83+
* [
84+
* '/user/login' => [
85+
* // METHOD => [...] // 这里 key 和 value里的 'methods' 是一样的
86+
* 'GET' => [
87+
* 'handler' => 'handler',
88+
* 'option' => [...],
89+
* ],
90+
* 'PUT' => [
91+
* 'handler' => 'handler',
92+
* 'option' => [...],
93+
* ],
94+
* ...
95+
* ],
96+
* ... ...
97+
* ]
98+
*/
99+
protected $staticRoutes = [];
100+
101+
/**
102+
* regular Routes - have dynamic arguments, but the first node is normal string.
103+
* 第一节是个静态字符串,称之为有规律的动态路由。按第一节的信息进行分组存储
104+
* e.g '/hello[/{name}]' '/user/{id}'
105+
* @var array[]
106+
* [
107+
* // 使用完整的第一节作为key进行分组
108+
* 'a' => [
109+
* [
110+
* 'start' => '/a/',
111+
* 'regex' => '/a/(\w+)',
112+
* 'methods' => 'GET,POST',
113+
* 'handler' => 'handler',
114+
* 'option' => [...],
115+
* ],
116+
* ...
117+
* ],
118+
* 'add' => [
119+
* [
120+
* 'start' => '/add/',
121+
* 'regex' => '/add/(\w+)',
122+
* 'methods' => 'GET',
123+
* 'handler' => 'handler',
124+
* 'option' => [...],
125+
* ],
126+
* ...
127+
* ],
128+
* 'blog' => [
129+
* [
130+
* 'start' => '/blog/post-',
131+
* 'regex' => '/blog/post-(\w+)',
132+
* 'methods' => 'GET',
133+
* 'handler' => 'handler',
134+
* 'option' => [...],
135+
* ],
136+
* ...
137+
* ],
138+
* ... ...
139+
* ]
140+
*/
141+
protected $regularRoutes = [];
142+
143+
/**
144+
* vague Routes - have dynamic arguments,but the first node is exists regex.
145+
* 第一节就包含了正则匹配,称之为无规律/模糊的动态路由
146+
* e.g '/{name}/profile' '/{some}/{some2}'
147+
* @var array
148+
* [
149+
* // 使用 HTTP METHOD 作为 key进行分组
150+
* 'GET' => [
151+
* [
152+
* // 必定包含的字符串
153+
* 'include' => '/profile',
154+
* 'regex' => '/(\w+)/profile',
155+
* 'handler' => 'handler',
156+
* 'option' => [...],
157+
* ],
158+
* ...
159+
* ],
160+
* 'POST' => [
161+
* [
162+
* 'include' => null,
163+
* 'regex' => '/(\w+)/(\w+)',
164+
* 'handler' => 'handler',
165+
* 'option' => [...],
166+
* ],
167+
* ...
168+
* ],
169+
* ... ...
170+
* ]
171+
*/
172+
protected $vagueRoutes = [];
173+
174+
/**
175+
* There are last route caches
176+
* @see $staticRoutes
177+
* @var array[]
178+
*/
179+
protected $routeCaches = [];
180+
79181
/**
80182
* object creator.
81183
* @param array $config
@@ -379,6 +481,13 @@ abstract protected function findInRegularRoutes(array $routesData, $path, $metho
379481
*/
380482
abstract protected function findInVagueRoutes(array $routesData, $path, $method);
381483

484+
/**
485+
* @param string $path
486+
* @param string $method
487+
* @param array $conf
488+
*/
489+
abstract protected function cacheMatchedParamRoute($path, $method, array $conf);
490+
382491
/**
383492
* handle auto route match, when config `'autoRoute' => true`
384493
* @param string $path The route path
@@ -526,4 +635,60 @@ public static function getSupportedMethods()
526635
{
527636
return self::SUPPORTED_METHODS;
528637
}
638+
639+
/**
640+
* @param array $staticRoutes
641+
*/
642+
public function setStaticRoutes(array $staticRoutes)
643+
{
644+
$this->staticRoutes = $staticRoutes;
645+
}
646+
647+
/**
648+
* @return array
649+
*/
650+
public function getStaticRoutes()
651+
{
652+
return $this->staticRoutes;
653+
}
654+
655+
/**
656+
* @param \array[] $regularRoutes
657+
*/
658+
public function setRegularRoutes(array $regularRoutes)
659+
{
660+
$this->regularRoutes = $regularRoutes;
661+
}
662+
663+
/**
664+
* @return \array[]
665+
*/
666+
public function getRegularRoutes()
667+
{
668+
return $this->regularRoutes;
669+
}
670+
671+
/**
672+
* @param array $vagueRoutes
673+
*/
674+
public function setVagueRoutes($vagueRoutes)
675+
{
676+
$this->vagueRoutes = $vagueRoutes;
677+
}
678+
679+
/**
680+
* @return array
681+
*/
682+
public function getVagueRoutes()
683+
{
684+
return $this->vagueRoutes;
685+
}
686+
687+
/**
688+
* @return array
689+
*/
690+
public function getRouteCaches()
691+
{
692+
return $this->routeCaches;
693+
}
529694
}

0 commit comments

Comments
 (0)