forked from brandonsavage/masterclass-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MasterController.php
52 lines (42 loc) · 1.36 KB
/
MasterController.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
<?php
class MasterController {
private $config;
public function __construct($config) {
$this->_setupConfig($config);
}
public function execute() {
$call = $this->_determineControllers();
$call_class = $call['call'];
$class = ucfirst(array_shift($call_class));
$method = array_shift($call_class);
$o = new $class($this->config);
return $o->$method();
}
private function _determineControllers()
{
if (isset($_SERVER['REDIRECT_BASE'])) {
$rb = $_SERVER['REDIRECT_BASE'];
} else {
$rb = '';
}
$ruri = $_SERVER['REQUEST_URI'];
$path = str_replace($rb, '', $ruri);
$return = array();
foreach($this->config['routes'] as $k => $v) {
$matches = array();
$pattern = '$' . $k . '$';
if(preg_match($pattern, $path, $matches))
{
$controller_details = $v;
$path_string = array_shift($matches);
$arguments = $matches;
$controller_method = explode('/', $controller_details);
$return = array('call' => $controller_method);
}
}
return $return;
}
private function _setupConfig($config) {
$this->config = $config;
}
}