-
Notifications
You must be signed in to change notification settings - Fork 5
/
router.php
81 lines (65 loc) · 1.82 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
<?php
class Router {
/**
* Contains controller name
* @var bool|string
*/
private $controller = false;
/**
* Contains method name
* @var bool|string
*/
private $method = false;
/**
* Contains args
* @var array
*/
private $args = array();
/**
* Loads routes from `app/routes.php`
*/
private function _load_routes() {
$routes_file = APPPATH . 'routes.php';
if (is_file($routes_file)) {
include($routes_file);
}
return empty($routes) ? array() : $routes;
}
/**
* Extracts path and sets `$controller`, `$method` and `$args`
*/
private function _extract_path($path) {
global $cfg;
$pathInfo = empty($path)? array() : explode('/', $path);
$this->controller = empty($pathInfo[1]) ? $cfg['default_controller'] : $pathInfo[1];
$this->method = empty($pathInfo[2]) ? $cfg['default_method'] : $pathInfo[2];
$this->args = array_filter(array_slice($pathInfo, 3));
}
function __construct($request_path) {
if (empty($request_path)) {
$request_path = '/';
}
// Clean up request path
$request_path = str_replace('\\', '/', $request_path);
$routes = $this->_load_routes();
$path = '';
foreach($routes as $from=>$to) {
$path = preg_replace('/^' . preg_quote($from, '/') . '/', $to, $request_path, 1, $count);
if ($count)
break;
}
if (empty($path)) {
$path = $request_path;
}
$this->_extract_path($path);
}
public function get_controller(){
return $this->controller;
}
public function get_method(){
return $this->method;
}
public function get_args(){
return $this->args;
}
}