forked from daveh/php-mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
214 lines (186 loc) · 5.92 KB
/
Router.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Core;
/**
* Router
*
* PHP version 7.0
*/
class Router
{
/**
* Associative array of routes (the routing table)
* @var array
*/
protected $routes = [];
/**
* Parameters from the matched route
* @var array
*/
protected $params = [];
/**
* Add a route to the routing table
*
* @param string $route The route URL
* @param array $params Parameters (controller, action, etc.)
*
* @return void
*/
public function add($route, $params = [])
{
// Convert the route to a regular expression: escape forward slashes
$route = preg_replace('/\//', '\\/', $route);
// Convert variables e.g. {controller}
$route = preg_replace('/\{([a-z]+)\}/', '(?P<\1>[a-z-]+)', $route);
// Convert variables with custom regular expressions e.g. {id:\d+}
$route = preg_replace('/\{([a-z]+):([^\}]+)\}/', '(?P<\1>\2)', $route);
// Add start and end delimiters, and case insensitive flag
$route = '/^' . $route . '$/i';
$this->routes[$route] = $params;
}
/**
* Get all the routes from the routing table
*
* @return array
*/
public function getRoutes()
{
return $this->routes;
}
/**
* Match the route to the routes in the routing table, setting the $params
* property if a route is found.
*
* @param string $url The route URL
*
* @return boolean true if a match found, false otherwise
*/
public function match($url)
{
foreach ($this->routes as $route => $params) {
if (preg_match($route, $url, $matches)) {
// Get named capture group values
foreach ($matches as $key => $match) {
if (is_string($key)) {
$params[$key] = $match;
}
}
$this->params = $params;
return true;
}
}
return false;
}
/**
* Get the currently matched parameters
*
* @return array
*/
public function getParams()
{
return $this->params;
}
/**
* Dispatch the route, creating the controller object and running the
* action method
*
* @param string $url The route URL
*
* @return void
*/
public function dispatch($url)
{
$url = $this->removeQueryStringVariables($url);
if ($this->match($url)) {
$controller = $this->params['controller'];
$controller = $this->convertToStudlyCaps($controller);
$controller = $this->getNamespace() . $controller;
if (class_exists($controller)) {
$controller_object = new $controller($this->params);
$action = $this->params['action'];
$action = $this->convertToCamelCase($action);
if (preg_match('/action$/i', $action) == 0) {
$controller_object->$action();
} else {
throw new \Exception("Method $action in controller $controller cannot be called directly - remove the Action suffix to call this method");
}
} else {
throw new \Exception("Controller class $controller not found");
}
} else {
throw new \Exception('No route matched.', 404);
}
}
/**
* Convert the string with hyphens to StudlyCaps,
* e.g. post-authors => PostAuthors
*
* @param string $string The string to convert
*
* @return string
*/
protected function convertToStudlyCaps($string)
{
return str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
}
/**
* Convert the string with hyphens to camelCase,
* e.g. add-new => addNew
*
* @param string $string The string to convert
*
* @return string
*/
protected function convertToCamelCase($string)
{
return lcfirst($this->convertToStudlyCaps($string));
}
/**
* Remove the query string variables from the URL (if any). As the full
* query string is used for the route, any variables at the end will need
* to be removed before the route is matched to the routing table. For
* example:
*
* URL $_SERVER['QUERY_STRING'] Route
* -------------------------------------------------------------------
* localhost '' ''
* localhost/? '' ''
* localhost/?page=1 page=1 ''
* localhost/posts?page=1 posts&page=1 posts
* localhost/posts/index posts/index posts/index
* localhost/posts/index?page=1 posts/index&page=1 posts/index
*
* A URL of the format localhost/?page (one variable name, no value) won't
* work however. (NB. The .htaccess file converts the first ? to a & when
* it's passed through to the $_SERVER variable).
*
* @param string $url The full URL
*
* @return string The URL with the query string variables removed
*/
protected function removeQueryStringVariables($url)
{
if ($url != '') {
$parts = explode('&', $url, 2);
if (strpos($parts[0], '=') === false) {
$url = $parts[0];
} else {
$url = '';
}
}
return $url;
}
/**
* Get the namespace for the controller class. The namespace defined in the
* route parameters is added if present.
*
* @return string The request URL
*/
protected function getNamespace()
{
$namespace = 'App\Controllers\\';
if (array_key_exists('namespace', $this->params)) {
$namespace .= $this->params['namespace'] . '\\';
}
return $namespace;
}
}