-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.php
73 lines (64 loc) · 2.29 KB
/
App.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
<?php
/**
* class App
* When created, it attempts to split the request URI into multiple pieces.
* After doing so, it delegates the rest of the requst to a controller.
*/
class App
{
private $url_controller = null;
private $url_action = null;
private $url_params = array();
// REGISTER CONTROLLERS HERE
private $validControllers = array('status', 'deploy');
/**
* App constructor splits the request URI into multiple pieces and finds a controller
* to delegate the rest of the work to
*/
public function __construct()
{
$this->splitUrl(); // Splits the URI
// Finds appropriate controller
if (!$this->url_controller) {
require_once CONTROLLERS . 'MainController.php';
$mainController = new MainController;
$mainController->render('index');
} else if (file_exists(APP . 'controllers/' . ucfirst($this->url_controller) . 'Controller.php')) {
$controllerName = ucfirst($this->url_controller) . 'Controller';
require_once CONTROLLERS . $controllerName . '.php';
$controller = new $controllerName();
$controller->render($this->url_action);
} else {
require_once CONTROLLERS . 'ErrorController.php';
$errorController = new ErrorController;
$errorController->render('index');
}
}
private function splitUrl()
{
$url = trim($_SERVER['REQUEST_URI'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
$validControllers = $this->validControllers;
if (isset($url[0]) && in_array($url[0], $validControllers)) {
$this->url_controller = $url[0];
} else if (isset($url[0])) {
$this->url_controller = 'main';
} else {
$this->url_controller = null;
}
if ($this->url_controller === 'main' && isset($url[0])) {
$this->url_action = $url[0];
} else if (isset($url[1])) {
$this->url_action = $url[1];
} else {
$this->url_action = null;
}
if ($this->url_controller === 'main') {
unset($url[0]);
} else {
unset($url[0], $url[1]);
}
$this->url_params = array_values($url);
}
}