Skip to content

Commit a238cbc

Browse files
committed
update readme, some docs
1 parent 43bb464 commit a238cbc

File tree

7 files changed

+299
-125
lines changed

7 files changed

+299
-125
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ git clone https://gitee.com/inhere/php-srouter.git // git@osc
6161

6262
详细的测试代码请看仓库 https://github.com/ulue/php-router-benchmark
6363

64+
- 压测日期 **2017.12.3**
6465
- An example route: `/9b37eef21e/{arg1}/{arg2}/{arg3}/{arg4}/{arg5}/{arg6}/{arg7}/{arg8}/{arg9}/bda37e9f9b`
6566

6667
压测结果
@@ -111,6 +112,7 @@ Macaw - first route (1000 routes) | 999 | 2.710132 | +2.703545 | 41047% slower
111112

112113
- `Inhere\Route\SRouter` 是静态类版本
113114
- `Inhere\Route\ORouter` 是对象版本
115+
- `Inhere\Route\CachedRouter` 是支持缓存的版本
114116

115117
两个类的方法名和参数都是一样的
116118

@@ -179,6 +181,9 @@ $router->group('/user', function () {
179181
// 使用 控制器
180182
$router->get('/', app\controllers\HomeController::class);
181183
$router->get('/index', 'app\controllers\HomeController@index');
184+
185+
// 可以注册一个备用路由处理。 当没匹配到时,就会使用它
186+
$router->any('*', 'fallback_handler');
182187
```
183188

184189
> 如果配置了 `'ignoreLastSep' => true`, '/index' 等同于 '/index/'

docs/benchmark.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
# 压测
3+
4+
自动生成了1000条路由,每条有9个参数位,分别测试1000次的
5+
6+
- 第一条路由匹配
7+
- 最后一条路由匹配
8+
- 不存在的路由匹配
9+
10+
详细的测试代码请看仓库 https://github.com/ulue/php-router-benchmark
11+
12+
- 压测日期 **2017.12.3**
13+
- An example route: `/9b37eef21e/{arg1}/{arg2}/{arg3}/{arg4}/{arg5}/{arg6}/{arg7}/{arg8}/{arg9}/bda37e9f9b`
14+
15+
压测结果
16+
17+
## Worst-case matching
18+
19+
Test Name | Results | Time(ms) | + Interval | Change
20+
--------- | ------- | ---- | ---------- | ------
21+
inhere/sroute(ORouter) - unknown route (1000 routes) | 987 | 0.010222 | +0.000000 | baseline
22+
inhere/sroute(SRouter) - unknown route (1000 routes) | 984 | 0.012239 | +0.002017 | 20% slower
23+
inhere/sroute(SRouter) - last route (1000 routes) | 999 | 0.024386 | +0.014820 | 155% slower
24+
inhere/sroute(ORouter) - last route (1000 routes) | 975 | 0.024554 | +0.014989 | 157% slower
25+
Symfony2 Cached - last route (1000 routes) | 997 | 0.029091 | +0.019525 | 204% slower
26+
Symfony2 Cached - unknown route (1000 routes) | 985 | 0.037226 | +0.027661 | 289% slower
27+
FastRoute - unknown route (1000 routes) | 988 | 0.089904 | +0.080338 | 840% slower
28+
FastRoute(cached) - unknown route (1000 routes) | 988 | 0.091358 | +0.081792 | 855% slower
29+
FastRoute(cached) - last route (1000 routes) | 999 | 0.092567 | +0.083001 | 868% slower
30+
FastRoute - last route (1000 routes) | 999 | 0.113668 | +0.104103 | 1088% slower
31+
phroute/phroute - unknown route (1000 routes) | 987 | 0.168871 | +0.159305 | 1665% slower
32+
phroute/phroute - last route (1000 routes) | 999 | 0.169914 | +0.160348 | 1676% slower
33+
Pux PHP - unknown route (1000 routes) | 981 | 0.866280 | +0.856714 | 8956% slower
34+
Pux PHP - last route (1000 routes) | 999 | 0.941322 | +0.931757 | 9741% slower
35+
AltoRouter - unknown route (1000 routes) | 982 | 2.245384 | +2.235819 | 23373% slower
36+
AltoRouter - last route (1000 routes) | 979 | 2.281995 | +2.272429 | 23756% slower
37+
Symfony2 - unknown route (1000 routes) | 984 | 2.488247 | +2.478681 | 25912% slower
38+
Symfony2 - last route (1000 routes) | 999 | 2.540170 | +2.530605 | 26455% slower
39+
Macaw - unknown route (1000 routes) | 982 | 2.617635 | +2.608069 | 27265% slower
40+
Macaw - last route (1000 routes) | 999 | 2.700128 | +2.690562 | 28127% slower
41+
42+
43+
## First route matching
44+
45+
Test Name | Results | Time(ms) | + Interval | Change
46+
--------- | ------- | ---- | ---------- | ------
47+
Pux PHP - first route(1000) | 997 | 0.006587 | +0.000000 | baseline
48+
FastRoute - first route(1000) | 999 | 0.008751 | +0.002165 | 33% slower
49+
phroute/phroute - first route (1000 routes) | 999 | 0.021902 | +0.015315 | 233% slower
50+
Symfony2 Dumped - first route | 997 | 0.022254 | +0.015667 | 238% slower
51+
ORouter - first route(1000) | 993 | 0.025026 | +0.018440 | 280% slower
52+
SRouter - first route(1000) | 997 | 0.025553 | +0.018967 | 288% slower
53+
noodlehaus/dispatch - first route (1000 routes) | 989 | 0.030126 | +0.023540 | 357% slower
54+
AltoRouter - first route (1000 routes) | 994 | 0.041488 | +0.034902 | 530% slower
55+
Symfony2 - first route | 991 | 0.047335 | +0.040748 | 619% slower
56+
FastRoute(cached) - first route(1000) | 999 | 0.092703 | +0.086117 | 1307% slower
57+
Macaw - first route (1000 routes) | 999 | 2.710132 | +2.703545 | 41047% slower

docs/router.md

Lines changed: 158 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
## 路由收集
1313

1414
```php
15-
use Inhere\Route\SRouter;
15+
use Inhere\Route\ORouter;
16+
17+
$router = new ORouter();
1618

1719
// 匹配 GET 请求. 处理器是个闭包 Closure
18-
SRouter::get('/', function() {
20+
$router->get('/', function() {
1921
echo 'hello';
2022
});
2123

2224
// 匹配参数 'test/john'
23-
SRouter::get('/test/{name}', function($arg) {
25+
$router->get('/test/{name}', function($arg) {
2426
echo $arg; // 'john'
2527
}, [
2628
'params' => [
@@ -29,7 +31,7 @@ SRouter::get('/test/{name}', function($arg) {
2931
]);
3032

3133
// 可选参数支持。匹配 'hello' 'hello/john'
32-
SRouter::get('/hello[/{name}]', function($name = 'No') {
34+
$router->get('/hello[/{name}]', function($name = 'No') {
3335
echo $name; // 'john'
3436
}, [
3537
'params' => [
@@ -38,53 +40,26 @@ SRouter::get('/hello[/{name}]', function($name = 'No') {
3840
]);
3941

4042
// 匹配 POST 请求
41-
SRouter::post('/user/login', function() {
43+
$router->post('/user/login', function() {
4244
var_dump($_POST);
4345
});
4446

4547
// 匹配 GET 或者 POST
46-
SRouter::map(['get', 'post'], '/user/login', function() {
48+
$router->map(['get', 'post'], '/user/login', function() {
4749
var_dump($_GET, $_POST);
4850
});
4951

5052
// 允许任何请求方法
51-
SRouter::any('/home', function() {
53+
$router->any('/home', function() {
5254
echo 'hello, you request page is /home';
5355
});
5456

5557
// 路由组
56-
SRouter::group('/user', function () {
57-
SRouter::get('/', function () {
58-
echo 'hello. you access: /user/';
59-
});
60-
SRouter::get('/index', function () {
61-
echo 'hello. you access: /user/index';
62-
});
63-
});
64-
```
65-
66-
使用 `ORouter` 则需先创建对象:
67-
68-
```php
69-
use Inhere\Route\ORouter;
70-
71-
$router = new ORouter;
72-
73-
// 添加路由
74-
// $router->get();
75-
// $router->post();
76-
// $router->put();
77-
// ... ...
7858
$router->group('/user', function ($router) {
7959
/** @var \Inhere\Route\ORouter $router */
80-
$router->get('', function () {
81-
echo 'hello. you access: /user';
60+
$router->get('/', function () {
61+
echo 'hello. you access: /user/';
8262
});
83-
84-
//$router->get('/', function () {
85-
// echo 'hello. you access: /user/';
86-
//});
87-
8863
$router->get('/index', function () {
8964
echo 'hello. you access: /user/index';
9065
});
@@ -99,23 +74,80 @@ $router->group('/user', function ($router) {
9974

10075
#### 1. 静态路由
10176

77+
整个路由 path 都是静态字符串 e.g. '/user/login'
78+
10279
例如:
10380

10481
```php
10582
$router->post('/user/signUp', 'handler2');
10683
```
10784

85+
- 存储结构
86+
87+
```php
88+
array (
89+
'/' => array (
90+
'GET' => array (
91+
'handler' => 'handler0',
92+
'option' => array (
93+
),
94+
),
95+
),
96+
'/home' => array (
97+
'GET' => array (
98+
'handler' => 'Inhere\\Route\\Examples\\Controllers\\HomeController@index',
99+
'option' => array (
100+
),
101+
),
102+
),
103+
'/post' => array (
104+
'POST' => array (
105+
'handler' => 'post_handler',
106+
'option' => array (
107+
),
108+
),
109+
),
110+
'/put' => array (
111+
'PUT' => array (
112+
'handler' => 'main_handler',
113+
'option' => array (
114+
),
115+
),
116+
),
117+
'/del' => array (
118+
'DELETE' => array (
119+
'handler' => 'main_handler',
120+
'option' => array (
121+
),
122+
),
123+
),
124+
'/pd' => array (
125+
'POST' => array (
126+
'handler' => 'multi_method_handler',
127+
'option' => array (
128+
),
129+
),
130+
'DELETE' => array (
131+
'handler' => 'multi_method_handler',
132+
'option' => array (
133+
),
134+
),
135+
),
136+
);
137+
```
138+
108139
#### 2. (有规律的)动态路由
109140

141+
第一节是个静态字符串,称之为有规律的动态路由。按第一节的信息进行分组存储
142+
110143
例如:
111144

112145
```php
113146
/*
114147
match:
115148
/hello/tom
116-
/hello
117149
*/
118-
$router->get('/hello[/{name}]', function($name='NO') {
150+
$router->get('/hello/{name}', function($name='NO') {
119151
echo "hello, $name"; // 'john'
120152
},[
121153
'params' => [
@@ -124,8 +156,47 @@ $router->get('/hello[/{name}]', function($name='NO') {
124156
]);
125157
```
126158

159+
- 存储结构
160+
161+
```php
162+
'user' => array (
163+
0 => array (
164+
'regex' => '#^/user/(?P<id>[1-9][0-9]*)$#',
165+
'start' => '/user/',
166+
'original' => '/user/{id}',
167+
'handler' => 'main_handler',
168+
'option' => array (
169+
),
170+
'methods' => 'GET',
171+
),
172+
1 => array (
173+
'regex' => '#^/user/(?P<id>[1-9][0-9]*)$#',
174+
'start' => '/user/',
175+
'original' => '/user/{id}',
176+
'handler' => 'main_handler',
177+
'option' => array (
178+
),
179+
'methods' => 'POST',
180+
),
181+
),
182+
'home' => array (
183+
0 => array (
184+
'regex' => '#^/home/(?P<act>[a-zA-Z][\\w-]+)$#',
185+
'start' => '/home/',
186+
'original' => '/home/{act}',
187+
'handler' => 'Inhere\\Route\\Examples\\Controllers\\HomeController',
188+
'option' => array (
189+
),
190+
'methods' => 'ANY,GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD,SEARCH,CONNECT,TRACE',
191+
),
192+
),
193+
)
194+
```
195+
127196
#### 3. (无规律的)动态路由
128197

198+
第一节就包含了正则匹配,称之为无规律/模糊的动态路由
199+
129200
例如:
130201

131202
```php
@@ -136,6 +207,52 @@ $router->get('/{name}', 'default_handler', [
136207
]);
137208
```
138209

210+
- 存储结构
211+
212+
```php
213+
array (
214+
'GET' => array (
215+
0 => array (
216+
'regex' => '#^/about(?:\\.html)?$#',
217+
'include' => '/about',
218+
'original' => '/about[.html]',
219+
'handler' => 'Inhere\\Route\\Examples\\Controllers\\HomeController@about',
220+
'option' => array (
221+
),
222+
),
223+
1 => array (
224+
'regex' => '#^/(?P<name>blog|saying)$#',
225+
'include' => NULL,
226+
'original' => '/{name}',
227+
'handler' => 'default_handler',
228+
'option' => array (
229+
'params' => array (
230+
'name' => 'blog|saying',
231+
),
232+
),
233+
),
234+
2 => array (
235+
'regex' => '#^/test(?:/optional)?$#',
236+
'include' => '/test',
237+
'original' => '/test[/optional]',
238+
'handler' => 'default_handler',
239+
'option' => array (
240+
),
241+
),
242+
3 => array (
243+
'regex' => '#^/blog-(?P<post>[^/]+)$#',
244+
'include' => '/blog-',
245+
'original' => '/blog-{post}',
246+
'handler' => 'default_handler',
247+
'option' => array (
248+
),
249+
),
250+
),
251+
'POST' => array( ... ),
252+
'PUT' => array( ... )
253+
)
254+
```
255+
139256
## 路由匹配
140257

141258
```php
@@ -154,7 +271,7 @@ array|false public function match($path, $method)
154271
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
155272
$method = $_SERVER['REQUEST_METHOD'];
156273

157-
$route = SRouter::match($path, $method);
274+
$route = $router->match($path, $method);
158275
```
159276

160277
匹配失败,返回 `false`
@@ -216,7 +333,7 @@ todo ...
216333

217334
```php
218335
// set config
219-
SRouter::setConfig([
336+
$router->setConfig([
220337
'ignoreLastSep' => true,
221338
'autoRoute' => 1,
222339
'controllerNamespace' => 'app\\controllers',
@@ -246,7 +363,7 @@ SRouter::setConfig([
246363
]
247364
```
248365

249-
> NOTICE: 必须在添加路由之前调用 `SRouter::setConfig()`
366+
> NOTICE: 必须在添加路由之前调用 `$router->setConfig()`
250367
251368
### 自动匹配路由
252369

examples/cached.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
function dump_routes() {
4040
global $router;
4141
echo "<h1>All Routes.</h1><pre><h2>StaticRoutes:</h2>\n";
42-
echo "<h2>RegularRoutes:</h2>\n";
4342
print_r($router->getStaticRoutes());
4443
echo "<h2>RegularRoutes:</h2>\n";
4544
print_r($router->getRegularRoutes());

0 commit comments

Comments
 (0)