-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouting.php
More file actions
159 lines (148 loc) · 4.89 KB
/
Copy pathRouting.php
File metadata and controls
159 lines (148 loc) · 4.89 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
<?php
// Central routing class responsible for directing HTTP requests to appropriate controllers and actions
require_once 'src/controllers/SecurityController.php';
require_once 'src/controllers/DashboardController.php';
require_once 'src/controllers/StoreController.php';
require_once 'src/controllers/GameController.php';
require_once 'src/controllers/LibraryController.php';
require_once 'src/controllers/AdminController.php';
require_once 'src/controllers/SupportController.php';
class Routing {
// Store controller instances (Singleton-like behavior)
private static array $instances = [];
// Application routes configuration
public static $routes = [
"login" => [
"controller" => "SecurityController",
"action" => "login"
],
"register" => [
"controller" => "SecurityController",
"action" => "register"
],
"logout" => [
"controller" => "SecurityController",
"action" => "logout"
],
"dashboard" => [
"controller" => "DashboardController",
"action" => "index"
],
"update-profile" => [
"controller" => "DashboardController",
"action" => "updateProfile"
],
"change-email" => [
"controller" => "DashboardController",
"action" => "changeEmail"
],
"change-password" => [
"controller" => "DashboardController",
"action" => "changePassword"
],
"delete-account" => [
"controller" => "DashboardController",
"action" => "deleteAccount"
],
"library" => [
"controller" => "LibraryController",
"action" => "index"
],
"game" => [
"controller" => "GameController",
"action" => "show"
],
"buy" => [
"controller" => "GameController",
"action" => "buy"
],
"toggle-wishlist" => [
"controller" => "GameController",
"action" => "toggleWishlist"
],
"toggle-like" => [
"controller" => "GameController",
"action" => "toggleReviewLike"
],
"add-review" => [
"controller" => "GameController",
"action" => "addReview"
],
"admin" => [
"controller" => "AdminController",
"action" => "index"
],
"edit-user" => [
"controller" => "AdminController",
"action" => "editUser"
],
"update-user" => [
"controller" => "AdminController",
"action" => "updateUser"
],
"change-role" => [
"controller" => "AdminController",
"action" => "changeRole"
],
"delete-user" => [
"controller" => "AdminController",
"action" => "deleteUser"
],
"add-game" => [
"controller" => "AdminController",
"action" => "addGame"
],
"edit-game" => [
"controller" => "AdminController",
"action" => "editGame"
],
"save-game" => [
"controller" => "AdminController",
"action" => "saveGame"
],
"update-featured" => [
"controller" => "AdminController",
"action" => "updateFeaturedGame"
],
"delete-game" => [
"controller" => "AdminController",
"action" => "deleteGame"
],
"support" => [
"controller" => "SupportController",
"action" => "index"
],
"" => [
"controller" => "StoreController",
"action" => "index"
]
];
// Return existing or create new controller instance
private static function getControllerInstance(string $controllerName) {
if (!isset(self::$instances[$controllerName])) {
self::$instances[$controllerName] = new $controllerName();
}
return self::$instances[$controllerName];
}
// Main routing logic
public static function run(string $path) {
// Support for routes like /dashboard/123
$parts = explode('/', trim($path, '/'));
$route = $parts[0] ?? '';
$id = $parts[1] ?? null;
// Check if route exists
if (array_key_exists($route, self::$routes)) {
$controllerName = self::$routes[$route]["controller"];
$action = self::$routes[$route]["action"];
// Get controller instance
$controller = self::getControllerInstance($controllerName);
// Call action with optional ID parameter
$controller->$action($id);
} else {
// Fallback 404 page
http_response_code(404);
$title = "404 - Page Not Found";
include 'public/views/errors/404.html';
}
}
}