-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
92 lines (78 loc) · 2.07 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
<?php
require_once 'controller/controller.php';
/**
* Routing class
*/
class Index extends Controller
{
/**
* The controller
* @var null
*/
public $url_controller = null;
/**
* The method (of the above controller)
* @var null
*/
public $url_method = null;
/**
* URL parameters
* @var array
*/
public $url_params = array();
public function __construct()
{
if (isset($_SERVER['REQUEST_URI'])) {
$this->splitUrl();
if ( ! $this->url_controller) {
$this->url_controller = 'login';
} else {
if ($this->checkController()) {
// Create controller and call method if they have been set in URL
$this->checkMethod() ? $this->createController($this->url_controller)->{$this->url_method}() : null;
} else {
$this->url_controller = 'error';
}
}
}
$this->createController($this->url_controller);
}
/**
* Split URL into the parts
*/
private function splitUrl()
{
$url = trim($_SERVER['REQUEST_URI'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
$this->url_controller = isset($url[0]) ? $url[0] : 'login';
$this->url_method = isset($url[1]) ? $url[1] : null;
unset($url[0], $url[1]);
$this->url_params = array_values($url);
}
/**
* Check controller existence
* @return boolean
*/
private function checkController()
{
if (file_exists('controller/' . $this->url_controller . '.php')) {
return true;
} else {
return false;
}
}
/**
* Check method existence
* @return boolean
*/
private function checkMethod()
{
if (method_exists($this->url_controller, $this->url_method)) {
return true;
} else {
return false;
}
}
}
new Index();