forked from dannyvankooten/PHP-Router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.php
153 lines (116 loc) · 4.54 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
<?php
/**
* Routing class to match request URL's against given routes and map them to a controller action.
*/
class Router {
/**
* Array that holds all Route objects
* @var array
*/
private $routes = array();
/**
* Array to store named routes in, used for reverse routing.
* @var array
*/
private $namedRoutes = array();
/**
* The base REQUEST_URI. Gets prepended to all route url's.
* @var string
*/
private $basePath = '';
/**
* Set the base url - gets prepended to all route url's.
* @param string $base_url
*/
public function setBasePath($basePath) {
$this->basePath = (string) $basePath;
}
/**
* Route factory method
*
* Maps the given URL to the given target.
* @param string $routeUrl string
* @param mixed $target The target of this route. Can be anything. You'll have to provide your own method to turn * this into a filename, controller / action pair, etc..
* @param array $args Array of optional arguments.
*/
public function map($routeUrl, $target = '', array $args = array()) {
$route = new Route();
$route->setUrl($this->basePath . $routeUrl);
$route->setTarget($target);
if(isset($args['methods'])) {
$methods = explode(',', $args['methods']);
$route->setMethods($methods);
}
if(isset($args['filters'])) {
$route->setFilters($args['filters']);
}
if(isset($args['name'])) {
$route->setName($args['name']);
$this->namedRoutes[$route->getName()] = $route;
}
$this->routes[] = $route;
}
/**
* Matches the current request against mapped routes
*/
public function matchCurrentRequest() {
$requestMethod = (isset($_POST['_method']) && ($_method = strtoupper($_POST['_method'])) && in_array($_method,array('PUT','DELETE'))) ? $_method : $_SERVER['REQUEST_METHOD'];
$requestUrl = $_SERVER['REQUEST_URI'];
// strip GET variables from URL
if(($pos = strpos($requestUrl, '?')) !== false) {
$requestUrl = substr($requestUrl, 0, $pos);
}
return $this->match($requestUrl, $requestMethod);
}
/**
* Match given request url and request method and see if a route has been defined for it
* If so, return route's target
* If called multiple times
*/
public function match($requestUrl, $requestMethod = 'GET') {
foreach($this->routes as $route) {
// compare server request method with route's allowed http methods
if(!in_array($requestMethod, $route->getMethods())) continue;
// check if request url matches route regex. if not, return false.
if (!preg_match("@^".$route->getRegex()."*$@i", $requestUrl, $matches)) continue;
$params = array();
if (preg_match_all("/:([\w-]+)/", $route->getUrl(), $argument_keys)) {
// grab array with matches
$argument_keys = $argument_keys[1];
// loop trough parameter names, store matching value in $params array
foreach ($argument_keys as $key => $name) {
if (isset($matches[$key + 1]))
$params[$name] = $matches[$key + 1];
}
}
$route->setParameters($params);
return $route;
}
return false;
}
/**
* Reverse route a named route
*
* @param string $route_name The name of the route to reverse route.
* @param array $params Optional array of parameters to use in URL
* @return string The url to the route
*/
public function generate($routeName, array $params = array()) {
// Check if route exists
if (!isset($this->namedRoutes[$routeName]))
throw new Exception("No route with the name $routeName has been found.");
$route = $this->namedRoutes[$routeName];
$url = $route->getUrl();
// replace route url with given parameters
if ($params && preg_match_all("/:(\w+)/", $url, $param_keys)) {
// grab array with matches
$param_keys = $param_keys[1];
// loop trough parameter names, store matching value in $params array
foreach ($param_keys as $i => $key) {
if (isset($params[$key]))
$url = preg_replace("/:(\w+)/", $params[$key], $url, 1);
}
}
return $url;
}
}