-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfrontcontroller.php
103 lines (85 loc) · 3.43 KB
/
frontcontroller.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
<?php
/*
* 先引导系统,然后单例方式实例化本前端控制器类(FC),FC负责解释请求的变量,接管程序的运行,分发路由至用户自定义的类,即动作控制器(AC)
*/
class FrontController {
private static $_instance = null;
protected $_controller;
protected $_action;
protected $_params = array();
// 防止直接new
private function __construct() {
// parse uri into string
$request_uri = $_SERVER['REQUEST_URI'];
$request_str = str_replace(Config::get('application.base_url'), '', $request_uri);
while(substr($request_str, strlen($request_str)-1) == '/') {
$request_str = substr($request_str, 0, -1);
}
// convert uri into array
$request_str_arr = explode('/', $request_str);
unset($request_uri);
unset($request_str);
// first two segments is controller/action
$this->_controller = empty($request_str_arr['0']) ? Config::get('application.default_controller') : $request_str_arr['0'];
$this->_action = empty($request_str_arr['1']) ? Config::get('application.default_action') : $request_str_arr['1'];
// uri parameters
for($i = 2; $i < count($request_str_arr); $i++)
{
$f = $i%2;
if($f == 0) $this->_params[$request_str_arr[$i]] = empty($request_str_arr[$i + 1]) ? null : $request_str_arr[$i + 1];
}
Log::info('Initialized FC successfully');
}
// 防止直接clone
private function __clone() {}
public static function getInstance() {
if(!(self::$_instance instanceof self)) {
self::$_instance = new FrontController();
}
return self::$_instance;
}
// 根据控制器和方法名称,执行控制器对应的方法
public function route() {
$request = new Request();
$request->url_elements = array();
// eg. /default/index
if(isset($_SERVER['PATH_INFO'])) {
$request->url_elements = explode('/', $_SERVER['PATH_INFO']);
}
$request->verb = $_SERVER['REQUEST_METHOD'];
switch($request->verb)
{
case 'GET':
$controllerName = ucfirst($this->_controller) . '_Controller';
$controllerHandler = new $controllerName();
$action = 'action_'.$this->_action;
if(!method_exists($controllerHandler, $action)){
throw new Exception('不存在方法:'.$action);
}
$controllerHandler->$action($this->_params);
exit;
case 'POST':
case 'PUT':
$request->parameters = json_decode(file_get_contents('php://input'), 1);
break;
case 'DELETE':
default:
$request->parameters = array();
}
if($request->url_elements) {
$controller_name = ucfirst($request->url_elements[1]) . '_Controller';
if(class_exists($controller_name)) {
$controller = new $controller_name();
$action_name = 'rest_' . ucfirst($request->verb);
$response = $controller->$action_name($request);
} else {
header('HTTP/1.0 400 Bad Request');
$response = "Unknown Request for " . $request->url_elements[1];
}
} else {
header('HTTP/1.0 400 Bad Request');
$response = "Unknown Request";
}
echo json_encode($response);
}
}