-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.php
More file actions
46 lines (34 loc) · 802 Bytes
/
Copy pathroutes.php
File metadata and controls
46 lines (34 loc) · 802 Bytes
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
<?php
include 'functions.php';
$uri = $_SERVER['REQUEST_URI'];
$method = $_SERVER['REQUEST_METHOD'];
$routes = [
'/cep' => [
'method' => 'GET',
'action' => 'buscaCep',
],
];
function checkRouteExist($routes, $uri)
{
if (!array_key_exists($uri, $routes)) {
http_response_code(404);
echo json_encode([
'data' => 'Not Found',
]);
die();
}
}
function checkMethod($routes, $uri, $method)
{
$routeMethod = $routes[$uri];
if ($routeMethod['method'] !== $method) {
http_response_code(405);
echo json_encode([
'data' => 'Method Not Allowed',
]);
die();
}
}
checkRouteExist($routes, $uri);
checkMethod($routes, $uri, $method);
call_user_func($routes[$uri]['action']);