-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
68 lines (54 loc) · 1.71 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
<?php
require_once 'config.php';
require_once 'database.php';
require_once 'helpers.php';
require_once 'core/Controller.php';
require_once 'core/Model.php';
require_once 'core/global_helpers.php';
// Get the request URI
$request_uri = $_SERVER['REQUEST_URI'];
// Remove query string if present
$request_uri = strtok($request_uri, '?');
// Extract the path after index.php
$path = explode('/index.php', $request_uri)[1] ?? '';
// Remove leading slash if present
$path = ltrim($path, '/');
// Split the path into segments
$segments = explode('/', $path);
// Set default controller and method
$controller = 'Home';
$method = 'index';
// If controller is specified in the URL, use it
if (!empty($segments[0])) {
$controller = ucfirst($segments[0]);
}
// If method is specified in the URL, use it
if (!empty($segments[1])) {
$method = $segments[1];
}
// Build the controller class name
$controller_class = $controller . 'Controller';
// Check if the controller file exists
$controller_file = __DIR__ . '/controllers/' . $controller_class . '.php';
if (file_exists($controller_file)) {
require_once $controller_file;
if (class_exists($controller_class)) {
$controller_instance = new $controller_class();
if (method_exists($controller_instance, $method)) {
// Call the method
$controller_instance->$method();
} else {
// Method not found
http_response_code(404);
echo "Method not found";
}
} else {
// Controller class not found
http_response_code(404);
echo "Controller not found";
}
} else {
// Controller file not found
http_response_code(404);
echo "Controller not found";
}