-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoute.php
More file actions
163 lines (147 loc) · 3.78 KB
/
Route.php
File metadata and controls
163 lines (147 loc) · 3.78 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
160
161
162
163
<?php
namespace Abdslam01\MiniFrameworkCore;
use Abdslam01\MiniFrameworkCore\Https\Request;
use Abdslam01\MiniFrameworkCore\Https\Response;
use Exception;
/**
* Route
*/
class Route {
private static $requests;
/**
* main
*
* @param mixed $path
* @param mixed $action
* @param mixed $request_method
* @param mixed $multiple
* @return Request
*/
private static function main(string $path, $action, string $request_method, bool $multiple=false){
$request = new Request($path, $action);
if($multiple)
foreach(explode(" ", "GET POST PUT PATCH DELETE OPTIONS") as $r_method)
self::$requests[$r_method][] = $request;
else
self::$requests[$request_method][] = $request;
return $request;
}
/**
* get
*
* @param mixed $path
* @param mixed $action
* @return Request
*/
public static function get(string $path, $action){
return self::main($path, $action, "GET");
}
/**
* post
*
* @param mixed $path
* @param mixed $action
* @return Request
*/
public static function post(string $path, $action){
return self::main($path, $action, "POST");
}
/**
* put
*
* @param mixed $path
* @param mixed $action
* @return Request
*/
public static function put(string $path, $action){
return self::main($path, $action, "PUT");
}
/**
* patch
*
* @param mixed $path
* @param mixed $action
* @return Request
*/
public static function patch(string $path, $action){
return self::main($path, $action, "PATCH");
}
/**
* delete
*
* @param mixed $path
* @param mixed $action
* @return Request
*/
public static function delete(string $path, $action){
return self::main($path, $action, "DELETE");
}
/**
* options
*
* @param mixed $path
* @param mixed $action
* @return Request
*/
public static function options(string $path, $action){
return self::main($path, $action, "OPTIONS");
}
/**
* all
*
* @param mixed $path
* @param mixed $action
* @return Request
*/
public static function all(string $path, $action){
return self::main($path, $action, "", true);
}
/**
* run
*
* @return void
*/
public static function run(){
foreach(self::$requests[$_SERVER['REQUEST_METHOD']]??[] as $request){
if($request->match(trim($_GET['url'], '/'))){
$request->execute();
die();
}
}
(new Response(404))->renderView();
}
/**
* url
*
* @param mixed $name
* @param mixed $params
* @return string|null
*/
public static function url(string $name, $params=[]){
foreach(self::$requests as $requests){
foreach($requests as $request){
if(array_key_exists($name, $request->name()??[])){
$path = $request->getPath();
foreach($params as $key=>$value){
$path = str_replace("{{$key}}", $value, $path);
}
break;
}
}
}
if(!isset($path)) throw new Exception("No route has the name: $name");
return BASE_URL.$path;
}
/**
* loadRoutes
*
* @return void
*/
public static function loadRoutes(){
$files = scandir(__DIR__."/../route/");
foreach($files as $file){
if(substr($file, -4)===".php")
require_once __DIR__."/../route/$file";
}
}
}