-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
188 lines (153 loc) · 4.63 KB
/
Copy pathRouter.php
File metadata and controls
188 lines (153 loc) · 4.63 KB
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
<?php
/**
* Router Class
*/
class Router{
/**
* Array that holds all routes
* @var array
*/
private static $arrRoutes = array();
/**
* String, base url
* @var array
*/
private static $strBaseUrl = "";
/**
* The base REQUEST_URI.
* @var string
*/
private static $strRequestUri = "";
/**
* The current request.
* @var string
*/
private static $strCurrentRequest = "";
/**
* Router dispatch method
*
* Maps the given URL to its target.
*/
public static function dispatch(){
$boolRouted = false; // did we find a route and successfully execute it?
self::$strCurrentRequest = str_replace(self::$strBaseUrl, "", self::$strRequestUri);
$arrMap = self::map();
if($arrMap){
$boolRouted = self::execute($arrMap);
var_dump($boolRouted);
}else{
// router fallback
}
if($boolRouted == false){
echo "404 BARF";
}
// Load controllers
}
/**
* Router mapping method
* Finds maps based on the routes
*
* strRoute represents the string with its params added by addRoute
* strCurrentRegex represents the regex string generated by the strRoute
* strCurrentRequest represents the current url
*/
public static function map(){
$strCurrentRequest = self::$strCurrentRequest;
$arrReturnMap = array();
// Loop trough routes
foreach(self::$arrRoutes as $strRoute => $arrMap){
// match current route on url segments and generate a regex out of it.
$strCurrentRegex = preg_replace("/:([\w-]+)/", "([\w-]+)", $strRoute);
// match generated regex on current request
$boolHasMatch = preg_match("@^".$strCurrentRegex."*$@i", $strCurrentRequest, $arrMatches);
if(!$boolHasMatch){
continue;
}else{
// extracting params to array
if (preg_match_all("/:([\w-]+)/", $strRoute, $arrParams)){
// get the matches
$arrParams = $arrParams[1];
$arrMapParams = array();
foreach ($arrParams as $strKey => $strName) {
// push param keys and values to array
if (isset($arrMatches[$strKey + 1])){
$arrMapParams[$strName] = $arrMatches[$strKey + 1];
}
}
if(isset($arrMapParams["action"])){
$arrMap["action"] = $arrMapParams["action"];
unset($arrMapParams["action"]);
}
$arrMap["params"] = $arrMapParams;
$arrReturnMap = $arrMap;
}else{
$arrMap["params"] = array();
$arrReturnMap = $arrMap;
}
}
}
if(count($arrReturnMap) == 0){
return false;
}
var_dump($arrReturnMap);
return $arrReturnMap;
}
/**
* Executes the route
* @param string $URI
*/
public static function execute($arrMap){
$strControllerPath = CONTROLLER_DIR . $arrMap["controller"] . CONTROLLER_SUFFIX . ".php";
$strControllerPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $strControllerPath;
$strController = $arrMap["controller"] . CONTROLLER_SUFFIX;
$strAction = METHOD_PREFIX . $arrMap["action"];
$arrParams = $arrMap["params"];
if(file_exists($strControllerPath)){
include_once($strControllerPath);
$objController = new $strController;
if(method_exists($objController, $strAction)){
call_user_func_array(array($objController, $strAction), $arrParams);
return true;
}else{
return false;
}
}else{
return false;
}
}
/**
* Router setBaseUrl method
*
* Adds route to router array.
* @param string url
*/
public static function setBaseUrl($strUrl){
self::$strBaseUrl = (string) $strUrl;
return true;
}
/**
* Sets the request uri
* @param string $URI
*/
public static function setRequestUri($strUri){
self::$strRequestUri = (string) $strUri;
return true;
}
/**
* Sets the request uri
* @param string $URI
*/
public static function execute404($strUri){
self::$strRequestUri = (string) $strUri;
return true;
}
/**
* Router addRoute method
*
* Adds route to router array.
* @param string $arrRoute
*/
public static function addRoute($strRoute, $arrMap){
self::$arrRoutes[$strRoute] = $arrMap;
}
}