forked from Tra-G/recipehaven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
58 lines (47 loc) · 1.49 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
<?php
// start session
session_start();
// Load the helper functions
require_once(__DIR__.'/models/helper.php');
// Include the routes
require_once('routes.php');
// Get the requested URL
$url = isset($_GET['url']) ? $_GET['url'] : '';
// Check if the requested URL is valid
$route = null;
foreach ($routes as $pattern => $routeConfig) {
if (preg_match($pattern, $url, $matches)) {
array_shift($matches); // Remove the full match from the matches array
$route = $routeConfig;
break;
}
}
if ($route) {
// If the URL is valid, redirect to the appropriate controller and action
$controller = $route['controller'];
$action = $route['action'];
$view = $route['view'];
require('controllers/' . $controller . '.php');
$controller = new $controller();
if (method_exists($controller, $action)) {
// If the action exists in the controller, execute it and pass the arguments
$data = $controller->$action(...$matches);
// If the action returns data, extract it for use in the view
if (isset($data)) {
extract($data);
}
// Load the appropriate view for the action
if ($view) {
require('views/' . $view . '.php');
}
} else {
// If the action does not exist in the controller, send 404 error code
http_response_code(404);
}
} else {
// If the URL is not valid, send 404 error code
http_response_code(404);
}
// close session
session_write_close();
?>