Just for fun——PHP框架之简单的路由器(1)

路由

路由的功能就是分发请求到不同的控制器,基于的原理就是正则匹配。接下来呢,我们实现一个简单的路由器,实现的能力是

  1. 对于静态的路由(没占位符的),正确调用callback
  2. 对于有占位符的路由,正确调用callback时传入占位符参数,譬如对于路由:/user/{id},当请求为/user/23时,传入参数$args结构为
1
2
3
复制代码[
'id' => '23'
]

大致思路

  1. 我们需要把每个路由的信息管理起来:http方法($method),路由字符串($route),回调($callback),因此需要一个addRoute方法,另外提供短方法get,post(就是把$method写好)
  2. 对于/user/{id}这样的有占位符的路由字符串,把占位符要提取出来,然后占位符部分变成正则字符串

实现

Route.php类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
复制代码<?php

namespace SalamanderRoute;

class Route {
/** @var string */
public $httpMethod;

/** @var string */
public $regex;

/** @var array */
public $variables;

/** @var mixed */
public $handler;

/**
* Constructs a route (value object).
*
* @param string $httpMethod
* @param mixed $handler
* @param string $regex
* @param array $variables
*/
public function __construct($httpMethod, $handler, $regex, $variables) {
$this->httpMethod = $httpMethod;
$this->handler = $handler;
$this->regex = $regex;
$this->variables = $variables;
}

/**
* Tests whether this route matches the given string.
*
* @param string $str
*
* @return bool
*/
public function matches($str) {
$regex = '~^' . $this->regex . '$~';
return (bool) preg_match($regex, $str);
}

}

Dispatcher.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
复制代码<?php
/**
* User: salamander
* Date: 2017/11/12
* Time: 13:43
*/

namespace SalamanderRoute;

class Dispatcher {
/** @var mixed[][] */
protected $staticRoutes = [];

/** @var Route[][] */
private $methodToRegexToRoutesMap = [];

const NOT_FOUND = 0;
const FOUND = 1;
const METHOD_NOT_ALLOWED = 2;

/**
* 提取占位符
* @param $route
* @return array
*/
private function parse($route) {
$regex = '~^(?:/[a-zA-Z0-9_]*|/\{([a-zA-Z0-9_]+?)\})+/?$~';
if(preg_match($regex, $route, $matches)) {
// 去掉full match
array_shift($matches);
return [
preg_replace('~{[a-zA-Z0-9_]+?}~', '([a-zA-Z0-9_]+)', $route),
$matches,
];
}
throw new \LogicException('register route failed, pattern is illegal');
}

/**
* 注册路由
* @param $httpMethod string | string[]
* @param $route
* @param $handler
*/
public function addRoute($httpMethod, $route, $handler) {
$routeData = $this->parse($route);
foreach ((array) $httpMethod as $method) {
if ($this->isStaticRoute($routeData)) {
$this->addStaticRoute($httpMethod, $routeData, $handler);
} else {
$this->addVariableRoute($httpMethod, $routeData, $handler);
}
}
}


private function isStaticRoute($routeData) {
return count($routeData[1]) === 0;
}

private function addStaticRoute($httpMethod, $routeData, $handler) {
$routeStr = $routeData[0];

if (isset($this->staticRoutes[$httpMethod][$routeStr])) {
throw new \LogicException(sprintf(
'Cannot register two routes matching "%s" for method "%s"',
$routeStr, $httpMethod
));
}

if (isset($this->methodToRegexToRoutesMap[$httpMethod])) {
foreach ($this->methodToRegexToRoutesMap[$httpMethod] as $route) {
if ($route->matches($routeStr)) {
throw new \LogicException(sprintf(
'Static route "%s" is shadowed by previously defined variable route "%s" for method "%s"',
$routeStr, $route->regex, $httpMethod
));
}
}
}

$this->staticRoutes[$httpMethod][$routeStr] = $handler;
}


private function addVariableRoute($httpMethod, $routeData, $handler) {
list($regex, $variables) = $routeData;

if (isset($this->methodToRegexToRoutesMap[$httpMethod][$regex])) {
throw new \LogicException(sprintf(
'Cannot register two routes matching "%s" for method "%s"',
$regex, $httpMethod
));
}

$this->methodToRegexToRoutesMap[$httpMethod][$regex] = new Route(
$httpMethod, $handler, $regex, $variables
);
}


public function get($route, $handler) {
$this->addRoute('GET', $route, $handler);
}

public function post($route, $handler) {
$this->addRoute('POST', $route, $handler);
}

public function put($route, $handler) {
$this->addRoute('PUT', $route, $handler);
}

public function delete($route, $handler) {
$this->addRoute('DELETE', $route, $handler);
}

public function patch($route, $handler) {
$this->addRoute('PATCH', $route, $handler);
}

public function head($route, $handler) {
$this->addRoute('HEAD', $route, $handler);
}

/**
* 分发
* @param $httpMethod
* @param $uri
*/
public function dispatch($httpMethod, $uri) {
$staticRoutes = array_keys($this->staticRoutes[$httpMethod]);
foreach ($staticRoutes as $staticRoute) {
if($staticRoute === $uri) {
return [self::FOUND, $this->staticRoutes[$httpMethod][$staticRoute], []];
}
}

$routeLookup = [];
$index = 1;
$regexes = array_keys($this->methodToRegexToRoutesMap[$httpMethod]);
foreach ($regexes as $regex) {
$routeLookup[$index] = [
$this->methodToRegexToRoutesMap[$httpMethod][$regex]->handler,
$this->methodToRegexToRoutesMap[$httpMethod][$regex]->variables,
];
$index += count($this->methodToRegexToRoutesMap[$httpMethod][$regex]->variables);
}
$regexCombined = '~^(?:' . implode('|', $regexes) . ')$~';
if(!preg_match($regexCombined, $uri, $matches)) {
return [self::NOT_FOUND];
}
for ($i = 1; '' === $matches[$i]; ++$i);
list($handler, $varNames) = $routeLookup[$i];
$vars = [];
foreach ($varNames as $varName) {
$vars[$varName] = $matches[$i++];
}
return [self::FOUND, $handler, $vars];
}
}

配置

nginx.conf重写到index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
复制代码location / {
try_files $uri $uri/ /index.php$is_args$args;

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}

composer.json自动载入

1
2
3
4
5
6
7
8
9
复制代码{
"name": "salmander/route",
"require": {},
"autoload": {
"psr-4": {
"SalamanderRoute\\": "SalamanderRoute/"
}
}
}

最终使用

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
复制代码<?php

include_once 'vendor/autoload.php';

use SalamanderRoute\Dispatcher;

$dispatcher = new Dispatcher();

$dispatcher->get('/', function () {
echo 'hello world';
});

$dispatcher->get('/user/{id}', function ($args) {
echo "user {$args['id']} visit";
});

// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

// 去掉查询字符串
if (false !== $pos = strpos($uri, '?')) {
$uri = substr($uri, 0, $pos);
}

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
case Dispatcher::NOT_FOUND:
echo '404 not found';
break;
case Dispatcher::FOUND:
$handler = $routeInfo[1];
$vars = $routeInfo[2];
$handler($vars);
break;
}

代码讲解,未完待续^–^

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%