/
index.php
75 lines (63 loc) · 1.81 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
<?php
$app = new App();
class App {
public static $config;
public function __construct() {
include('config/config.php');
include('core/Controller.php');
self::$config = $config;
$this->route();
}
public static function show404()
{
header("HTTP/1.0 404 Not Found");
exit();
}
private function route() {
$params = explode('/', strtok($_SERVER["REQUEST_URI"],'?'));
$filePath = $this->parseURL($params);
$class = $this->loadClass($filePath);
$this->executeMethod($class, $params);
}
private function parseURL($params)
{
if (!isset($_GET['backend_request']) || empty($params[1])) {
$directory = self::$config['index_directory'];
$controller = self::$config['index_controller'];
} else {
$directory = $params[1];
$controller = $params[2];
}
$filePathArr = array(
'controllers',
$directory,
$controller . '.php',
);
$filePath = implode('/', $filePathArr);
if (!file_exists($filePath)) {
$this->show404();
}
return $filePath;
}
public static function loadClass($filePath)
{
include($filePath);
$className = end(get_declared_classes());
$class = new $className();
return $class;
}
private function executeMethod($class, $params)
{
if (!isset($params[3])) {
$class->index();
} else {
$this->runMethodWithParams($class, $params);
}
}
private function runMethodWithParams($class, $params)
{
$methodParams = array_slice($params, 4);
call_user_func_array(array($class, $params[3]), $methodParams);
}
}
?>