-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
77 lines (55 loc) · 2.52 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
<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$url = isset($_SERVER['PATH_INFO']) ? explode('/', ltrim($_SERVER['PATH_INFO'],'/')) : '/';
//set up header
require_once __DIR__.'/Views/header_view.php';
$headerView = New HeaderView();
if ($url == '/')
{
// This is the home page
// Initiate the home controller
// and render the home view
require_once __DIR__.'/Models/index_model.php';
require_once __DIR__.'/Controllers/index_controller.php';
require_once __DIR__.'/Views/index_view.php';
$indexModel = New IndexModel();
$indexController = New IndexController($indexModel);
$indexView = New IndexView($indexController, $indexModel);
}else{
// 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 );
}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
}
}
//set up footer
require_once __DIR__.'/Views/footer_view.php';
$footerView = New FooterView();