... | ... |
@@ -234,11 +234,16 @@ class Application{ |
234 | 234 |
} |
235 | 235 |
|
236 | 236 |
public function handle404($request){ |
237 |
- echo "Resource not found!<br>".PHP_EOL; |
|
238 |
- echo "<pre>"; |
|
239 |
- var_dump($request); |
|
240 |
- echo "</pre>"; |
|
241 |
- throw new \Exception("Not implemented!"); |
|
237 |
+ if($action = $this->router->setMatch404($request)){ |
|
238 |
+ $this->runAction($action); |
|
239 |
+ } |
|
240 |
+ else{ |
|
241 |
+ echo "Resource not found!<br>".PHP_EOL; |
|
242 |
+ echo "<pre>"; |
|
243 |
+ var_dump($request); |
|
244 |
+ echo "</pre>"; |
|
245 |
+ throw new \Exception("The 404 (not found) route not set!"); |
|
246 |
+ } |
|
242 | 247 |
} |
243 | 248 |
|
244 | 249 |
public function beforeAction(){ |
... | ... |
@@ -272,52 +277,56 @@ class Application{ |
272 | 277 |
} |
273 | 278 |
} |
274 | 279 |
|
275 |
- public function run(){ |
|
276 |
- // Check if routing path is found |
|
277 |
- if($action = $this->router->match($this->request)){ |
|
278 |
- // routing match found decide what to do next... |
|
279 |
- // BeforeAction event |
|
280 |
- $this->beforeAction(); |
|
280 |
+ public function runAction($action){ |
|
281 |
+ // routing match found decide what to do next... |
|
282 |
+ // BeforeAction event |
|
283 |
+ $this->beforeAction(); |
|
281 | 284 |
|
282 |
- // function -> call it... |
|
283 |
- if(is_callable($action)){ |
|
284 |
- $reflection = new \ReflectionFunction($action); |
|
285 |
- if($reflection->getNumberOfParameters()){ |
|
286 |
- $fire_args=array(); |
|
287 |
- |
|
288 |
- foreach($reflection->getParameters() AS $arg) |
|
289 |
- { |
|
290 |
- if(isset($this->router->parsedParameters[$arg->name])) |
|
291 |
- $fire_args[$arg->name]=$this->router->parsedParameters[$arg->name]; |
|
292 |
- else |
|
293 |
- $fire_args[$arg->name]=null; |
|
294 |
- } |
|
295 |
- |
|
296 |
- $result = call_user_func_array($action, $fire_args); |
|
297 |
- } |
|
298 |
- else{ |
|
299 |
- $result = call_user_func($action); |
|
285 |
+ // function -> call it... |
|
286 |
+ if(is_callable($action)){ |
|
287 |
+ $reflection = new \ReflectionFunction($action); |
|
288 |
+ if($reflection->getNumberOfParameters()){ |
|
289 |
+ $fire_args=array(); |
|
290 |
+ |
|
291 |
+ foreach($reflection->getParameters() AS $arg) |
|
292 |
+ { |
|
293 |
+ if(isset($this->router->parsedParameters[$arg->name])) |
|
294 |
+ $fire_args[$arg->name]=$this->router->parsedParameters[$arg->name]; |
|
295 |
+ else |
|
296 |
+ $fire_args[$arg->name]=null; |
|
300 | 297 |
} |
301 |
- } |
|
302 |
- else if(is_object($action) && is_a($action, 'L2_controler_info')){ |
|
303 |
- // not implemented yet. |
|
304 |
- throw new \Exception("Not implemented!"); |
|
305 |
- ///$action->runControlerAction($this->routing->param); |
|
306 |
- } |
|
307 |
- // array with controller, and action -> run the controller action |
|
308 |
- else if(is_array($action) && array_key_exists('controller',$action) && array_key_exists('action', $action)){ |
|
309 |
- $result = $this->runControllerAction($action, $this->router->parsedParameters); |
|
310 | 298 |
|
299 |
+ $result = call_user_func_array($action, $fire_args); |
|
311 | 300 |
} |
312 |
- // string |
|
313 |
- else if(is_string($action)){ |
|
314 |
- $result = $action; |
|
301 |
+ else{ |
|
302 |
+ $result = call_user_func($action); |
|
315 | 303 |
} |
304 |
+ } |
|
305 |
+ else if(is_object($action) && is_a($action, 'L2_controler_info')){ |
|
306 |
+ // not implemented yet. |
|
307 |
+ throw new \Exception("Not implemented!"); |
|
308 |
+ ///$action->runControlerAction($this->routing->param); |
|
309 |
+ } |
|
310 |
+ // array with controller, and action -> run the controller action |
|
311 |
+ else if(is_array($action) && array_key_exists('controller',$action) && array_key_exists('action', $action)){ |
|
312 |
+ $result = $this->runControllerAction($action, $this->router->parsedParameters); |
|
313 |
+ |
|
314 |
+ } |
|
315 |
+ // string |
|
316 |
+ else if(is_string($action)){ |
|
317 |
+ $result = $action; |
|
318 |
+ } |
|
316 | 319 |
|
317 |
- // Handle the action result |
|
318 |
- if(isset($result)){ |
|
319 |
- $this->handleActionResult($result); |
|
320 |
- } |
|
320 |
+ // Handle the action result |
|
321 |
+ if(isset($result)){ |
|
322 |
+ $this->handleActionResult($result); |
|
323 |
+ } |
|
324 |
+ } |
|
325 |
+ |
|
326 |
+ public function run(){ |
|
327 |
+ // Check if routing path is found |
|
328 |
+ if($action = $this->router->match($this->request)){ |
|
329 |
+ $this->runAction($action); |
|
321 | 330 |
} |
322 | 331 |
// routing path not found -> generate 404 response |
323 | 332 |
else{ |
... | ... |
@@ -2,6 +2,7 @@ |
2 | 2 |
|
3 | 3 |
namespace elanpl\L3; |
4 | 4 |
|
5 |
+define('_L3_ROUTE_404_NAME', '_L3_404'); |
|
5 | 6 |
class Router{ |
6 | 7 |
|
7 | 8 |
protected $routes; // The defined routes collection |
... | ... |
@@ -24,6 +25,10 @@ class Router{ |
24 | 25 |
return $this; |
25 | 26 |
} |
26 | 27 |
|
28 |
+ public function add404($result){ |
|
29 |
+ return $this->add('404', '', $result, _L3_ROUTE_404_NAME); |
|
30 |
+ } |
|
31 |
+ |
|
27 | 32 |
public function get($path, $result, $name = ''){ |
28 | 33 |
return $this->add('GET', $path, $result, $name); |
29 | 34 |
} |
... | ... |
@@ -109,6 +114,28 @@ class Router{ |
109 | 114 |
return false; |
110 | 115 |
} |
111 | 116 |
|
117 |
+ public function getRouteInfo($name){ |
|
118 |
+ if(isset($this->routeNameIndex[$name])) |
|
119 |
+ return $this->routeNameIndex[$name]; |
|
120 |
+ else |
|
121 |
+ return false; |
|
122 |
+ } |
|
123 |
+ |
|
124 |
+ public function calculateRequestDepth($request){ |
|
125 |
+ return count(explode('/',trim($request->path, "/ \t\n\r\0\x0B"))); |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ public function setMatch404($request){ |
|
129 |
+ if($routeInfo404 = $this->getRouteInfo(_L3_ROUTE_404_NAME)){ |
|
130 |
+ $this->depth = $this->calculateRequestDepth($request); |
|
131 |
+ $this->routeInfo = $routeInfo404; |
|
132 |
+ return $routeInfo404->result; |
|
133 |
+ } |
|
134 |
+ else{ |
|
135 |
+ return false; |
|
136 |
+ } |
|
137 |
+ } |
|
138 |
+ |
|
112 | 139 |
public function link($name, $parameters){ |
113 | 140 |
$route = $this->routeNameIndex[$name]; |
114 | 141 |
$fields = array_keys($parameters); |