-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
94 lines (73 loc) · 3.28 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
<?php
session_start();
require_once 'config/database.php';
require_once 'config/global.php';
$url = isset($_SERVER['PATH_INFO']) ? explode('/', ltrim($_SERVER['PATH_INFO'], '/')) : '/';
if ($url == '/') {
// This is the home page
// Initiate the home controller
// and render the home view
require_once __DIR__.'/Models/home.model.php';
require_once __DIR__.'/Controllers/home.controller.php';
require_once __DIR__.'/Views/home.view.php';
$indexModel = new HomeModel();
$indexController = new HomeController($indexModel);
$indexView = new HomeView($indexController, $indexModel);
print $indexView->index();
} else {
try { //Catch Fatal Error by unknows requestedParams
// This is not home page
// Initiate the appropriate controller
// and render the required view
//The first element should be a controller
$requestedController = $url[0];
// If a second part is added in the URI,
// it should be a method
$requestedAction = isset($url[1])? $url[1] :'';
// The remain parts are considered as
// arguments of the method
$requestedParams = array_slice($url, 2);
// Check if controller exists. NB:
// You have to do that for the model and the view too
$ctrlPath = __DIR__.'/Controllers/'.$requestedController.'.controller.php';
if (file_exists($ctrlPath)) {
require_once __DIR__.'/Models/'.$requestedController.'.model.php';
require_once __DIR__.'/Controllers/'.$requestedController.'.controller.php';
require_once __DIR__.'/Views/'.$requestedController.'.view.php';
$modelName = ucfirst($requestedController).'Model';
$controllerName = ucfirst($requestedController).'Controller';
$viewName = ucfirst($requestedController).'View';
$controllerObj = new $controllerName(new $modelName);
$viewObj = new $viewName($controllerObj, new $modelName);
// If there is a method - Second parameter
if ($requestedAction != '') {
// then we call the method via the view
// dynamic call of the view
print $viewObj->$requestedAction($requestedParams);
} else {
print $viewObj->index();
}
} else {
//header('HTTP/1.1 404 Not Found');
//die('404 - The file - '.$ctrlPath.' - not found');
//require the 404 controller and initiate it
//Display its view
require_once __DIR__.'/Models/404.php';
require_once __DIR__.'/Controllers/404.php';
require_once __DIR__.'/Views/404.php';
$NotFoundModel = new NotFoundModel();
$NotFoundController = new NotFoundController($NotFoundModel);
$NotFoundView = new NotFoundView($NotFoundController, $NotFoundModel);
print $NotFoundView->index();
}
} catch (\Throwable $e) {
/*require_once __DIR__.'/Models/404.php';
require_once __DIR__.'/Controllers/404.php';
require_once __DIR__.'/Views/404.php';
$NotFoundModel = new NotFoundModel();
$NotFoundController = new NotFoundController($NotFoundModel);
$NotFoundView = new NotFoundView($NotFoundController, $NotFoundModel);
print $NotFoundView->index();*/
die(print $e);
}
}