Skip to content

Commit 26d39d0

Browse files
committed
feat: 重组路由信息
1 parent e0aa4f7 commit 26d39d0

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

index.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@
33
define('APP_ROOT', strtr(__DIR__, '\\', '/') . '/');
44

55
require APP_ROOT . 'library/autoload.php';
6-
7-
App::boot(
8-
$argv[1] ?? array_shift($_GET)
9-
);
6+
App::boot($argv[1] ?? reset($_GET));

library/app/app.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22

33
class App
44
{
5-
/**
6-
* @var AppRouter $router
7-
*/
8-
public static $router;
9-
105
/**
116
* 启动指定模块
127
* @param string $url
138
* @return void
149
*/
1510
public static function boot($url)
1611
{
17-
// 注册页面路由器
18-
self::$router = new AppRouter();
19-
require APP_MODULES . 'route.php';
20-
$match = self::$router->match($url ?: '/');
21-
call_user_func($match['target'], $match['params'])->output();
12+
$router = new AppRouter();
13+
if (is_file(APP_MODULES . 'route.php')) {
14+
require APP_MODULES . 'route.php';
15+
}
16+
// 回退路由
17+
$router->map('GET', '*', function ($args) {
18+
$model = new ErrorModel($args);
19+
$model->warning('not found', $args);
20+
return $model;
21+
});
22+
// 匹配路由
23+
$request = $router->match($url ?: '/');
24+
$request['target']($request['params'])->output();
2225
}
2326

2427
/**

modules/route.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
// 默认首页
44

5-
self::$router->map('GET', '/', function ($args) {
5+
$router->map('GET', '/', function ($args) {
66
$model = new HomeModel();
77
$model->view($args);
88
return $model;
99
});
1010

1111
// 重建缓存
1212

13-
self::$router->map('GET', '/admin/build', function ($args) {
13+
$router->map('GET', '/admin/build', function ($args) {
1414
$model = new AdminModel();
1515
$model->build($args);
1616
return $model;
@@ -21,18 +21,10 @@
2121
foreach (App::cache('index') as $category) {
2222
foreach ($category['routes'] as $route) {
2323
$rt = $route + ['cid' => $category['id']];
24-
self::$router->map($rt['method'], $rt['route'], function ($args) use ($rt) {
24+
$router->map($rt['method'], $rt['route'], function ($args) use ($rt) {
2525
$model = new $rt['model'](); // 初始化模型
2626
$model->{$rt['action']}($rt + $args);
2727
return $model;
2828
});
2929
}
3030
}
31-
32-
// 回退路由
33-
34-
self::$router->map('GET', '*', function ($args) {
35-
$model = new ErrorModel($args);
36-
$model->warning('not found', $args);
37-
return $model;
38-
});

0 commit comments

Comments
 (0)