-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoute.class.php
82 lines (76 loc) · 2.01 KB
/
Route.class.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
<?php
class Route
{
public $Modifier = 'i'; //路由规则匹配时 的 正则修饰符 i 不区分大小写 u 贪婪模式关闭
public $url; // URL类
public $path; // 实际路由
public $segments; // 实际路由 拆解 成数组
public $module = BIND_MODULE;
public $class = BIND_CLASS;
public $method = 'index';
public function __construct()
{
$this->routes = include_once 'Route.config.php';
$this->url = URL::getInstance();
}
/**
* 解析路由 将域名路由 映射成 真路由
* @param [type] $path [域名路由]
* @return [type] [返回真正路由]
*/
public function parseRoute($path)
{
if (!empty($this->routes)) {
foreach ($this->routes as $k => $v) {
$key = str_replace([':any', ':num'], ['.*', '[0-9]+'], $k);
if (preg_match('#^' . $key . '$#' . $this->Modifier, $path, $matches)) {
if (strpos($v, '$') !== false && strpos($key, '(') !== false) {
$v = preg_replace('#^' . $key . '$#i', $v, $path);
}
$path = $v;
break;
}
}
}
return $path;
}
/**
* 设置 请求的 module class method 以及 参数
* @param array $segments [description]
*/
public function setRequest($segments = [])
{
isset($segments[1]) && $this->setModule($segments[1]);
isset($segments[2]) && $this->setClass($segments[2]);
isset($segments[3]) && $this->setMethod($segments[3]);
$this->setArgv(array_slice($segments,3));
return ;
}
public function setModule($module)
{
$this->module = $module;
}
public function setClass($class)
{
$this->class = str_replace(['/', '.'], '', $class);
}
public function setMethod($method)
{
$this->method = $method;
}
/**
* 剩余的参数部分保存至argvs中
* @param [type] $argvs [description]
*/
public function setArgv($argvs)
{
$data = [];
if(is_array($argvs) && !empty($argvs)){
$argvs_num = count($argvs);
for ($i = 0; $i < $argvs_num ;$i = $i + 2) {
isset($argvs[$i+1]) ? $data[$argvs[$i]] = $argvs[$i+1] : '';
}
}
$this->argvs = $data;
}
}