-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframework.php
140 lines (138 loc) · 4.54 KB
/
framework.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
<?php
class Core
{
public function __construct()
{
/**
* 自动运行加载子级的__init()对象
*/
if (method_exists($this, "__init")) {
$this->__init();
}
}
public function run()
{
spl_autoload_register(array(
__CLASS__,
'autoload'
));
//启动路由规则
Routes::route();
//执行伪路由规则
Routes::path_info();
date_default_timezone_set('PRC'); //设置中国时区
}
private function autoload($classname)
{
if (preg_match("/Controller/", $classname)) {
$classpath = APP_CONTROLLER . $_GET['m'] . "/" . $classname . '.class.php';
} else {
$classpath = APP_EXTENDS . $classname . '.class.php';
}
if (file_exists($classpath)) {
require_once($classpath);
} else if (APP_DEBUG) {
die($classname . '控制器不存在!');
} else {
die('error');
}
}
}
class Routes
{
//PathInfo 路由规则
static public function PathInfo()
{
$_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();
}
}