-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.php
112 lines (98 loc) · 2.43 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
error_reporting(E_ALL);
if (!defined('APPPATH')) {
define('APPPATH', realpath(dirname(__FILE__) . '/../app' ). '/');
}
define('SYSPATH', realpath(dirname(__FILE__) . '/') . '/');
/*
* Require app config
*/
$app_cfg_file = APPPATH . 'config.php';
if (is_file($app_cfg_file)) {
include(APPPATH . 'config.php');
}
/*
* Set unset configs
*/
if (!isset($cfg) || !is_array($cfg)) {
$cfg = array();
}
$default_cfg = array(
'default_controller' => 'hello',
'default_method' => 'index',
'base_url' => '',
'databases' => [
// 'name' => [
// 'db_host' => '',
// 'db_user' => '',
// 'db_pass' => '',
// 'db_name' => '',
// ]
],
);
foreach ($default_cfg as $key => $value) {
if (empty($cfg[$key])) {
$cfg[$key] = $value;
}
}
/*
* Set $_base_url
*/
if (empty($cfg['base_url'])) {
$_REQUEST_SCHEME = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') ? 'https' : 'http';
$_PATH = dirname($_SERVER['SCRIPT_NAME']);
if (substr($_PATH, -1) != '/') $_PATH .= '/';
$_base_url = $_REQUEST_SCHEME.'://'.$_SERVER['HTTP_HOST'].$_PATH;
}
else {
$_base_url = $cfg['base_url'];
}
/*
* Helper implementation
*/
function load_helper($helper) {
$helper_file = APPPATH . 'helpers/'.$helper.'.php';
if (is_file($helper_file)) {
include_once($helper_file);
}
else {
include_once(SYSPATH . 'helpers/'.$helper.'.php');
}
}
/*
* Load common helpers
*/
load_helper('base_url');
/*
* Load system
*/
require(SYSPATH . 'router.php');
require(SYSPATH . 'loader.php');
require(SYSPATH . 'controller.php');
require(SYSPATH . 'model.php');
require(SYSPATH . 'library.php');
/*
* Understand URL and decompose route
*/
$router = new Router(empty($_SERVER['PATH_INFO']) ? '' : $_SERVER['PATH_INFO']);
$controller = $router->get_controller();
$method = $router->get_method();
$args = $router->get_args();
/*
* Load requested controller
*/
$not_found = true;
$controller_file = APPPATH . 'controllers/'.$controller.'.php';
if (file_exists($controller_file) && is_readable($controller_file)) {
include($controller_file);
$a=new $controller;
if (method_exists($a, $method)) {
$not_found = false;
call_user_func_array( array( $a, $method ), $args );
}
}
if ($not_found) {
http_response_code(404);
Loader::view(404);
exit();
}