-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoute.class.php
100 lines (99 loc) · 3.52 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
class Routes
{
//PathInfo 路由规则
static public function path_info()
{
$_GET['m'] = empty($_GET['m']) ? 'index' : $_GET['m'];
$_GET['c'] = empty($_GET['c']) ? 'index' : $_GET['c'];
//执行l 路由规则
self::Lroute();
!empty($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] = $_SERVER['PATH_INFO'];
if (!empty($_SERVER['PATH_INFO'])) {
$path = $_SERVER['PATH_INFO'];
$get = explode('/', $path);
unset($get[0]);
foreach ($get as $key => $value) {
if ($key > 0) {
if (count($get) == 1) {
$_GET['c'] = $get[1];
} else if (count($get) == 2) {
$_GET['c'] = $get[1];
$_GET['a'] = $get[2];
} else if (count($get) > 2) {
$_GET['m'] = $get[1];
$_GET['c'] = $get[2];
$_GET['a'] = $get[3];
}
}
if (count($get) > 4) {
$param = array_slice($get, 3);
$param = array_filter($param);
for ($i = 0; $i < count($param); $i += 2) {
if ($param[$i] != 'm' && $param[$i] != 'a' && $param[$i] != 'c' && !empty($param[$i + 1])) {
$_GET[$param[$i]] = $param[$i + 1];
}
}
}
}
}
}
//l url访问模式处理器
static private function Lroute()
{
if (!empty($_GET['l'])) {
$l = $_GET['l'];
$l = explode('/', $l);
if (count($l) >= 3) {
$_GET['m'] = !empty($l[0]) ? $l[0] : '';
$_GET['c'] = !empty($l[1]) ? $l[1] : '';
$_GET['a'] = !empty($l[2]) ? $l[2] : '';
} else if (count($l) == 2) {
$_GET['c'] = !empty($l[0]) ? $l[0] : '';
$_GET['a'] = !empty($l[1]) ? $l[1] : '';
} else if (count($l) == 1) {
$_GET['c'] = 'index';
$_GET['a'] = !empty($l[1]) ? $l[1] : '';
}
if (count($l) > 3) {
$param = array_slice($l, 3);
$param = array_filter($param);
for ($i = 0; $i < count($param); $i += 2) {
if ($param[$i] != 'm' && $param[$i] != 'a' && $param[$i] != 'c' && !empty($param[$i + 1])) {
$_GET[$param[$i]] = $param[$i + 1];
}
}
}
}
}
//路由处理函数
static public function route()
{
//判断访问模式
if (!empty($_SERVER['PATH_INFO'])) {
$_SESSION['urlmodel'] = 2;
} else if (!empty($_GET['l'])) {
$_SESSION['urlmodel'] = 3;
} else {
$_SESSION['urlmodel'] = 1;
}
// self::path_info(); l=index/index/index
if (!empty($_GET['c']) && !empty($_GET['a'])) {
$c = ucfirst($_GET['c']) . "Controller";
$a = ucfirst($_GET['a']);
} else if (!empty($_GET['c'])) {
$c = ucfirst($_GET['c']) . "Controller";
$a = 'index';
} else {
$c = 'index' . "Controller";
$a = 'index';
}
$con = new $c;
if (!method_exists($con, $a)) {
die($a . '方法不存在!');
} else if (!method_exists($con, $a)) {
die('error');
}
$con->$a();
}
}