-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.php
79 lines (68 loc) · 1.77 KB
/
Router.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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class Router
{
public function match($path, $callback)
{
$request = $_SERVER['REQUEST_URI'];
if ($request == $path) {
$callback();
exit();
}
}
public function api($path, $file)
{
$request = $_SERVER['REQUEST_URI'];
if ($request == $path) {
require __DIR__ . '/api/' . $file . '.php';
exit();
}
}
public function render($path, $file)
{
$request = $_SERVER['REQUEST_URI'];
if ($request == $path) {
require __DIR__ . '/views/' . $file . '.php';
exit();
}
}
public function route($path, $Router)
{
$request = $_SERVER['REQUEST_URI'];
if (str_starts_with($request, $path)) {
require __DIR__ . '/routes/' . $Router . '.php';
}
}
public function params($path, $callback)
{
$request = $_SERVER['REQUEST_URI'];
if (str_starts_with($request, $path)) {
$params = explode($path, $request);
if (strlen($params[1])) {
$callback($params[1]);
} else {
$callback(null);
}
}
}
public function catch($path, $callback)
{
$callback();
}
public function setPublic($path)
{
$request = $_SERVER['REQUEST_URI'];
if (str_starts_with($request, $path)) {
if (!file_exists(__DIR__ . $path)) {
header("HTTP/1.0 404 Not Found");
} else {
$file = __DIR__ . $path;
readfile($file);
header("HTTP/1.0 200 OK");
}
}
}
}
$Router = new Router;