-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.php
More file actions
162 lines (135 loc) · 3.83 KB
/
index.php
File metadata and controls
162 lines (135 loc) · 3.83 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
// Check if path is available or not empty
if(isset($_SERVER['PATH_INFO'])){
$path= $_SERVER['PATH_INFO'];
// Do a path split
$path_split = explode('/', ltrim($path));
}else{
// Set Path to '/'
$path_split = '/';
}
if($path_split === '/'){
/* match with index route
* Import IndexController and match requested method with it
*/
require_once __DIR__.'/Models/indexModel.php';
require_once __DIR__.'/Controllers/indexController.php';
$req_model = new IndexModel();
$req_controller = new IndexController($req_model);
/**
*Model and Controller assignment with first letter as UPPERCASE
*@return Class;
*/
$model = $req_model;
$controller = $req_controller;
/**
*Creating an Instance of the the model and the controller each
*@return Object;
*/
$ModelObj = new $model;
$ControllerObj = new $controller($req_model);
/**
*Assigning Object of Class Init to a Variable, to make it Usable
*@return Method Name;
*/
$method = $req_method;
/**
*Check if Controller Exist is not empty, then performs an
*action on the method;
*@return true;
*/
if ($req_method != '')
{
/**
*Outputs The Required controller and the req *method respectively
*@return Required Method;
*/
print $ControllerObj->$method($req_param);
}
else
{
/**
*This works in only when the Url doesnt have a parameter
*@return void;
*/
print $ControllerObj->index();
}
}else{
// fetch corresponding controller
$req_controller = $path_split[1];
/**
*Set Required Model name
*@return model;
*/
$req_model = $path_split[1];
/**
*Set Required Method name
*@return method;
*/
$req_method = isset($path_split[2])? $path_split[2] :'';
/**
*Set Required Params
*@return params;
*/
$req_param = array_slice($path_split, 3);
/**
*Check if Controller Exist
*@return void;
*/
$req_controller_exists = __DIR__.'/Controllers/'.$req_controller.'Controller.php';
if (file_exists($req_controller_exists))
{
/**
*Requiring all the files needed i.e The Corresponding Model and Controller
*@return corresponding class respectively;
*/
require_once __DIR__.'/Models/'.$req_model.'Model.php';
require_once __DIR__.'/Controllers/'.$req_controller.'Controller.php';
/**
*Model and Controller assignment with first letter as UPPERCASE
*@return Class;
*/
$model = ucfirst($req_model).'Model';
$controller = ucfirst($req_controller).'Controller';
/**
*Creating an Instance of the the model and the controller each
*@return Object;
*/
$ModelObj = new $model;
$ControllerObj = new $controller(ucfirst($req_model.'Model'));
/**
*Assigning Object of Class Init to a Variable, to make it Usable
*@return Method Name;
*/
$method = $req_method;
/**
*Check if Controller Exist is not empty, then performs an
*action on the method;
*@return true;
*/
if ($req_method != '')
{
/**
*Outputs The Required controller and the req *method respectively
*@return Required Method;
*/
print $ControllerObj->$method($req_param);
}
else
{
/**
*This works in only when the Url doesnt have a parameter
*@return void;
*/
print $ControllerObj->index();
}
}
else
{
header('HTTP/1.1 404 Not Found');
die('404 - The file - '.$app->req_controller.' - not found');
//require the 404 controller and initiate it
//Display its view
}
}
?>