-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
79 lines (62 loc) · 2.09 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
<?php
//Handle the URL Request
$request_url = $_SERVER['REQUEST_URI'];
//Define Paths
define('ROOT_DIR', dirname(__FILE__) . '/');
$root = explode('/', substr($request_url, 1));
define('ROOT_URL', '/' . $root[0] . '/');
//Include config file
require_once('config/db.config.php');
//Include DB Class
require_once('includes/db.class.php');
//Init DB Class
$db = new DB_Class();
//Include UserAuth Class
require_once('includes/auth.class.php');
//Include Main Controller
include_once('controllers/MainController.php');
//Include Main Model
include_once('models/BaseModel.php');
//Define default controller
$controller = 'Main';
$action = 'render';
// Extract data from the HTTP request
$requestPath = parse_url($request_url, PHP_URL_PATH);
$requestPath = substr( $requestPath, strlen( ROOT_URL ) );
$requestParts = explode('/', $requestPath);
if (count($requestParts) >= 1 && $requestParts[0] != '') {
$controller = strtolower($requestParts[0]);
if (! preg_match('/^[a-zA-Z0-9_]+$/', $controller)) {
die('Invalid controller name. Use letters, digits and underscore only.');
}
}
if (count($requestParts) >= 2 && $requestParts[1] != '') {
$action = $requestParts[1];
if (! preg_match('/^[a-zA-Z0-9_]+$/', $action)) {
die('Invalid action name. Use letters, digits and underscore only.');
}
}
$params = [];
if (count($requestParts) >= 3) {
$params = array_splice($requestParts, 2);
}
if ( isset( $controller ) && file_exists( 'controllers/' . ucfirst($controller) . 'Controller.php' ) ) {
//Handle if Admin Area
$is_admin = '';
$admin_folder = '';
$admin_path = $is_admin ? 'admin/' : '';
include_once 'controllers/' . $admin_folder . ucfirst($controller) . 'Controller.php';
$controller_class = ucfirst( $controller ) . '_Controller';
$instance = new $controller_class();
// Call the Action
if( method_exists( $instance, $action ) ) {
call_user_func_array( array( $instance, $action ), array( $params ) );
} else {
call_user_func_array( array( $instance, 'index' ), array() );
}
} else {
//Init Main controller
$main = new Main_Controller();
$main->render();
}
?>