-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
66 lines (63 loc) · 2.02 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
<?php
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
spl_autoload_register('apiAutoload');
function apiAutoload($classname)
{
if ($classname == 'Controller'){
return false;
} elseif (preg_match('/[a-zA-Z]+Controller$/', $classname)) {
include __DIR__ . '/controllers/' . $classname . '.php';
return true;
} elseif (preg_match('/[a-zA-Z]+Model$/', $classname)) {
include __DIR__ . '/models/' . $classname . '.php';
return true;
} elseif (preg_match('/[a-zA-Z]+View$/', $classname)) {
include __DIR__ . '/views/' . $classname . '.php';
return true;
} else {
include __DIR__ . '/library/' . str_replace('_', DIRECTORY_SEPARATOR, $classname) . '.php';
return true;
}
return false;
}
$request = new Request();
try{
// route the request to the right place
$controllerElement = 0;
if(isset($request->url_elements[$controllerElement])){
$controller_name = ucfirst($request->url_elements[$controllerElement]) . 'Controller';
if (class_exists($controller_name)) {
http_response_code(200);
//$data = $request->parameters;
$controller = new $controller_name();
$controller->element = $controllerElement+1;
$action_name = strtolower($request->verb) . 'Action';
if(method_exists($controller, $action_name)) {
$result = $controller->$action_name($request);
} else {
$result = array(
'status' => 'ERROR',
'message' => 'Request method unavailable!'
);
}
$view_name = ucfirst($request->format) . 'View';
if(class_exists($view_name)) {
$view = new $view_name();
$view->render($result);
}
} else {
http_response_code(404);
}
} else {
http_response_code(404);
}
} catch(Exception $e){
$result['status'] = 'ERROR';
$result['message'] = $e->getMessage();
$view_name = ucfirst($request->format) . 'View';
if(class_exists($view_name)) {
$view = new $view_name();
$view->render($result);
}
}
?>