forked from felicity-iiith/minimalMVC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
137 lines (120 loc) · 2.99 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?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' => '',
// ]
],
'i18n' => [
'locales' => [
// 'en_IN',
],
'languages' => [
// 'en' => 'en_IN'
],
'gettext' => false
// 'gettext' => [
// 'domain' => 'messages',
// 'directory' => './locale'
// ]
],
'404_view' => '404',
'404_data' => [
// data to pass to a generic error view in case of a 404
],
);
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'];
}
/*
* Locale aware base_url, will be updated by the i18n class
*/
$_locale_base_url = $_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');
require(SYSPATH . 'i18n.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($cfg['404_view'], $cfg['404_data']);
exit();
}