Skip to content

Commit eeb2994

Browse files
committed
update readme, bug fixed for like 'blog-{name}'
1 parent f2bf113 commit eeb2994

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ SRouter::group('/user', function () {
154154
echo 'hello. you access: /user/index';
155155
});
156156
});
157+
158+
// 使用 控制器
159+
SRouter::get('/', app\controllers\HomeController::class);
160+
SRouter::get('/index', 'app\controllers\HomeController@index');
157161
```
158162

159163
> 如果配置了 `'ignoreLastSep' => true`, '/index' 等同于 '/index/'
@@ -358,10 +362,10 @@ $dispatcher->on('notFound', function ($uri) {
358362
通过`@`符号连接控制器类和方法名可以指定执行方法。
359363

360364
```php
361-
SRouter::get('/', app\controllers\Home::class);
365+
SRouter::get('/', app\controllers\HomeController::class);
362366

363-
SRouter::get('/index', 'app\controllers\Home@index');
364-
SRouter::get('/about', 'app\controllers\Home@about');
367+
SRouter::get('/index', 'app\controllers\HomeController@index');
368+
SRouter::get('/about', 'app\controllers\HomeController@about');
365369
```
366370

367371
> NOTICE: 若第二个参数仅仅是个 类,将会尝试执行通过 `defaultAction` 配置的默认方法
@@ -373,11 +377,11 @@ SRouter::get('/about', 'app\controllers\Home@about');
373377
> NOTICE: 使用动态匹配控制器方法, 应当使用 `any()` 添加路由. 即此时无法限定请求方法 `REQUEST_METHOD`
374378
375379
```php
376-
// 访问 '/home/test' 将会执行 'app\controllers\Home::test()'
377-
SRouter::any('/home/{any}', app\controllers\Home::class);
380+
// 访问 '/home/test' 将会执行 'app\controllers\HomeController::test()'
381+
SRouter::any('/home/{any}', app\controllers\HomeController::class);
378382

379383
// 可匹配 '/home', '/home/test' 等
380-
SRouter::any('/home[/{name}]', app\controllers\Home::class);
384+
SRouter::any('/home[/{name}]', app\controllers\HomeController::class);
381385
```
382386

383387
> NOTICE: 上面两个的区别是 第一个无法匹配 `/home`
@@ -392,16 +396,16 @@ SRouter::any('/home[/{name}]', app\controllers\Home::class);
392396
示例:
393397

394398
```php
395-
// 访问 '/user', 将会调用 app\controllers\User::run('')
396-
SRouter::get('/user', 'app\controllers\User');
399+
// 访问 '/user', 将会调用 app\controllers\UserController::run('')
400+
SRouter::get('/user', 'app\controllers\UserController');
397401

398-
// 访问 '/user/profile', 将会调用 app\controllers\User::run('profile')
399-
SRouter::get('/user/profile', 'app\controllers\User');
402+
// 访问 '/user/profile', 将会调用 app\controllers\UserController::run('profile')
403+
SRouter::get('/user/profile', 'app\controllers\UserController');
400404

401405
// 同时配置 'actionExecutor' => 'run' 和 'dynamicAction' => true,
402-
// 访问 '/user', 将会调用 app\controllers\User::run('')
403-
// 访问 '/user/profile', 将会调用 app\controllers\User::run('profile')
404-
SRouter::any('/user[/{name}]', 'app\controllers\User');
406+
// 访问 '/user', 将会调用 app\controllers\UserController::run('')
407+
// 访问 '/user/profile', 将会调用 app\controllers\UserController::run('profile')
408+
SRouter::any('/user[/{name}]', 'app\controllers\UserController');
405409
```
406410

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

docs/router.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ $router->group('/user', function ($router) {
9393

9494
> 如果配置了 `'ignoreLastSep' => true`, '/index' 等同于 '/index/'
9595
96-
### 如何收集
96+
### 实现原理(如何收集)
9797

9898
添加的路由将会分为三类 `静态路由` `(有规律的)动态路由` `(无规律的)动态路由`
9999

@@ -202,6 +202,8 @@ $route = SRouter::match($path, $method);
202202

203203
### 匹配原理
204204

205+
匹配优先级按 `cached -> static -> regular -> vague -> autoRoute(if it is enabled)`
206+
205207
todo ...
206208

207209
## 路由配置
@@ -210,11 +212,9 @@ todo ...
210212
// set config
211213
SRouter::setConfig([
212214
'ignoreLastSep' => true,
213-
'autoRoute' => [
214-
'enable' => 1,
215-
'controllerNamespace' => 'app\\controllers',
216-
'controllerSuffix' => 'Controller',
217-
],
215+
'autoRoute' => 1,
216+
'controllerNamespace' => 'app\\controllers',
217+
'controllerSuffix' => 'Controller',
218218
]);
219219
```
220220

examples/some-routes.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ function my_handler(array $args) {
8686
'/test[/optional]',
8787
'default_handler'
8888
],
89+
[
90+
'GET',
91+
'/blog-{post}',
92+
'default_handler'
93+
],
94+
[
95+
'GET',
96+
'/blog[index]',
97+
'default_handler'
98+
],
8999
/*
90100
match:
91101
/my/tom/78
@@ -152,6 +162,11 @@ function () {
152162
echo 'hello. you access: /user/index';
153163
}
154164
],
165+
[
166+
'GET',
167+
'/{some}',
168+
'default_handler'
169+
],
155170
]
156171
]
157172

src/AbstractRouter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ public static function parseParamRoute($route, array $params, array $conf)
177177
// e.g '/hello[/{name}]' first: 'hello', '/user/{id}' first: 'user', '/a/{post}' first: 'a'
178178
// first node is a normal string
179179
// if (preg_match('#^/([\w-]+)#', $tmp, $m)) {
180-
if (preg_match('#^/([\w-]+)/?[\w-]*#', $tmp, $m)) {
180+
// if (preg_match('#^/([\w-]+)/?[\w-]*#', $tmp, $m)) {
181+
if (preg_match('#^/([\w-]+)/[\w-]*#', $tmp, $m)) {
181182
$first = $m[1];
182183
$info = [
183184
'regex' => $regex,

src/SRouter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ public static function match($path, $method = self::GET)
258258
return self::findInStaticRoutes(self::$staticRoutes[$path], $path, $method);
259259
}
260260

261-
$tmp = trim($path, '/'); // clear first,end '/'
262-
$first = strpos($tmp, '/') ? strstr($tmp, '/', true) : $tmp;
261+
$first = self::getFirstFromPath($path);
263262

264263
// is a regular dynamic route(the first char is 1th level index key).
265264
if (isset(self::$regularRoutes[$first])) {

0 commit comments

Comments
 (0)