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
|
<?php
/**
* Handles client request.
* @package Core
*/
class Request {
/**
* Stores POST data
* @var array
* @access private
*/
private $_post;
/**
* Stores GET data
* @var array
* @access private
*/
private $_get;
/**
* Current Route
* @var Route
* @access public
*/
public $route;
/**
* Request method
* @var string
* @access public
*/
public $method;
/**
* Retrieves a GET parameter
*
* @param string $key Parameter key
* @param mixed $default Default value
* @return mixed Returns a value if a key is specified,
* or an array of GET parameters if it isn't.
* @access public
*/
public function get($key = null, $default = null) {
if ($key == null)
return $this->_get;
return Misc::arr($this->_get,$key,$default);
}
/**
* Retrieves a POST parameter
*
* @param string $key Parameter key
* @param mixed $default Default value
* @return mixed Returns a value if a key is specified,
* or an array of POST parameters if it isn't.
* @access public
*/
public function post($key = null, $default = null) {
if ($key == null)
return $this->_post;
return Misc::arr($this->_post,$key,$default);
}
/**
* Retrieves a Route parameter
*
* @param string $key Parameter key
* @param mixed $default Default value
* @return mixed Returns a value if a key is specified,
* or an array of Route parameters if it isn't.
* @access public
*/
public function param($key = null, $default = null) {
if ($key == null)
return $this->route->params;
return Misc::arr($this->route->params,$key,$default);
}
/**
* Initializes the routed Controller and executes specified action
*
* @return Response A Response object with the body and headers set
* @access public
*/
public function execute() {
$controller = $this->param('controller').'_Controller';
if (!class_exists($controller))
throw new Exception("Class {$controller} doesn't exist",404);
$controller = new $controller;
$controller->request = $this;
$controller->run($this->param('action'));
return $controller->response;
}
/**
* Initializes the Request and process the URI into a Route
*
* @return object Request
* @access public
* @static
*/
public static function create(){
$request = new Request();
$request->_post = $_POST;
$request->_get = $_GET;
$uri = $_SERVER['REQUEST_URI'];
$basepath=Config::get('core.basepath','/');
$uri = preg_replace("#^{$basepath}index\.php#i", '', $uri);
$url_parts = parse_url($uri);
$request->route = Route::match($url_parts['path']);
$request->method=$_SERVER['REQUEST_METHOD'];
return $request;
}
}
|