-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.php
More file actions
64 lines (53 loc) · 1.49 KB
/
index.php
File metadata and controls
64 lines (53 loc) · 1.49 KB
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
<?php
// Default allow requests from everywhere. You can remove it if you want
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');
require 'constants.php';
require __DIR__ . '/vendor/autoload.php';
use SimpleORM\app\controller;
$uri = $_SERVER['REQUEST_URI'];
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET': $the_request = &$_GET;
break;
case 'POST': $the_request = &$_POST;
break;
default:
$the_request = null;
}
if ($uri == '/') {
$response = ['response' => 'No content to show'];
echo json_encode($response);
exit;
}
$src = explode('/', $uri);
$model = ucfirst($src[1]);
$controller = $model . 'Controller';
$method = (isset($src[2])) ? $src[2] : 'index';
if (isset($src[3]) && empty($the_request)) {
$the_request = filter_var($src[3], FILTER_SANITIZE_STRING);
}
/*
* call current class/method
*/
try {
$load_class = 'SimpleORM\app\controller\\' . $controller;
$class = new $load_class();
$set = $class->$method($the_request);
} catch (Exception $e) {
echo 'No ' . $controller . ' found for this route', $e->getMessage(), "\n";
}
/*
* Declare all variables if passed in return
*/
if ( ! empty($set) && is_array($set)) {
foreach ($set as $k => $v) {
${$k} = $v;
}
}
/*
* If method has a view file, include
*/
$view_file = APP_VIEW . $model . DS . $method . PHP;
if (file_exists($view_file)) {
include $view_file;
}