-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.php
More file actions
133 lines (114 loc) · 3.38 KB
/
Copy pathrouter.php
File metadata and controls
133 lines (114 loc) · 3.38 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
<?php
class Route
{
private static string $Url;
private static array $Parameters = [];
public static function any($Url, $Params)
{
self::$Url = $Url;
if (self::CheckURL()) {
self::InitController($Params);
}
}
public static function get($Url, $Params)
{
// Add Url to class
self::$Url = $Url;
// Check request to be GET
if ($_SERVER['REQUEST_METHOD'] === 'GET' && self::CheckURL()) {
self::InitController($Params);
}
}
public static function post($Url, $Params)
{
// Add Url to class
self::$Url = $Url;
// Check request to be POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && self::CheckURL()) {
self::InitController($Params);
}
}
public static function put($Url, $Params)
{
// Add Url to class
self::$Url = $Url;
// Check request to be PUT
if ($_SERVER['REQUEST_METHOD'] === 'POST' &&
isset($_POST['_method']) &&
$_POST['_method'] === 'PUT' &&
self::CheckURL()) {
self::InitController($Params);
}
}
public static function patch($Url, $Params)
{
// Add Url to class
self::$Url = $Url;
// Check request to be PATCH
if ($_SERVER['REQUEST_METHOD'] === 'POST' &&
isset($_POST['_method']) &&
$_POST['_method'] === 'PATCH' &&
self::CheckURL()) {
self::InitController($Params);
}
}
public static function delete($Url, $Params)
{
// Add Url to class
self::$Url = $Url;
// Check request to be DELETE
if ($_SERVER['REQUEST_METHOD'] === 'POST' &&
isset($_POST['_method']) &&
$_POST['_method'] === 'DELETE' &&
self::CheckURL()) {
self::InitController($Params);
}
}
private static function InitController($Params)
{
$Params = explode('@', $Params);
$Controller = $Params[0];
$Method = $Params[1];
require_once __DIR__ . "/controller/{$Controller}.php";
$ClassInit = new $Controller();
$ClassInit->$Method();
if (property_exists($ClassInit, 'Scripts')):
$_SESSION['Scripts'] = $ClassInit->Scripts;
else:
$_SESSION['Scripts'] = [];
endif;
}
private static function CheckURL(): bool
{
// uri that programmer define
$Uri = array_values(array_filter(explode('/', self::$Url)));
//client url
$Url = array_filter(explode('/', $_GET['param']));
/**
* Url and Uri are not at same size.
* so, not this one :))
* @return false
*/
if (count($Uri) !== count($Url)):
return false;
endif;
/**
* check parameter by every section
* if there is variable in uri, it will add to $Parameter array
*/
foreach ($Uri as $Key => $Params):
//if url has {*} accept whatever in it
if (preg_match("/\{(.*?)\}/", $Params)):
$Param = str_replace(['{', '}'], '', $Params);
self::$Parameters[$Param] = $Url[$Key];
continue;
else:
if ($Params !== $Url[$Key]):
return false;
endif;
endif;
endforeach;
// client url truly found
return true;
}
}