-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
109 lines (103 loc) · 3.34 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
<?php
define('DB_PATH', $_SERVER['DOCUMENT_ROOT'] . '/db/database.db');
// Load the autoloader
require_once __DIR__ . '/framework/Autoload.php';
// Define the existing website routes
$routes = [
'/' => [
'controller' => 'Controller@index',
'middlewares' => []
],
'/login' => [
'controller' => 'Controller@login',
'middlewares' => []
],
'/messages' => [
'controller' => 'Controller@messages',
'middlewares' => [new AuthenticationMiddleware(), new BannedMiddleware()]
],
'/search' => [
'controller' => 'Controller@search',
'middlewares' => []
],
'/profile' => [
'controller' => 'Controller@profile',
'middlewares' => [new AuthenticationMiddleware(), new BannedMiddleware()]
],
'/product' => [
'controller' => 'Controller@product',
'middlewares' => []
],
'/banned' => [
'controller' => 'Controller@banned',
'middlewares' => []
],
'/settings' => [
'controller' => 'Controller@settings',
'middlewares' => [new AuthenticationMiddleware(), new BannedMiddleware()]
],
'/checkout' => [
'controller' => 'Controller@checkout',
'middlewares' => [new AuthenticationMiddleware(), new BannedMiddleware()]
],
'/help' => [
'controller' => 'Controller@help',
'middlewares' => []
],
'/about' => [
'controller' => 'Controller@about',
'middlewares' => []
],
'/cookie-policy' => [
'controller' => 'Controller@cookiePolicy',
'middlewares' => []
],
'/privacy-policy' => [
'controller' => 'Controller@privacyPolicy',
'middlewares' => []
],
'/terms-and-conditions' => [
'controller' => 'Controller@termsAndConditions',
'middlewares' => []
],
'/api' => [
'controller' => 'ApiController@handle',
'middlewares' => []
],
'/dashboard' => [
'controller' => 'Controller@dashboard',
'middlewares' => [new AdminMiddleware(), new AuthenticationMiddleware(), new BannedMiddleware()]
],
'/new-product' => [
'controller' => 'Controller@newProduct',
'middlewares' => [new AuthenticationMiddleware(), new BannedMiddleware()]
],
'/edit-product' => [
'controller' => 'Controller@editProduct',
'middlewares' => [new AuthenticationMiddleware(), new BannedMiddleware()]
]
];
// Extract the path from the URL and compare it to the defined routes
$request_uri = $_SERVER['REQUEST_URI'];
$path = parse_url($request_uri, PHP_URL_PATH);
[ , $location, $args[] ] = explode('/', $path);
$route = $routes['/' . $location] ?? null;
// Create the Request object associated
$request = new Request();
if ($route) {
// Select which controller and action to load
list($controllerName, $actionName) = explode('@', $route['controller']);
// Process the middleware chain (if it exists)
foreach ($route['middlewares'] as $middleware) {
$request = $middleware->handle($request, function ($request) {
return $request;
});
}
// Create the controller and generate page
$controller = new $controllerName($request);
echo $controller->$actionName($args);
} else {
// Display 404 page if route is not defined
require_once __DIR__ . '/pages/404_page.php';
draw404Page($request);
}