-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
71 lines (61 loc) · 2.07 KB
/
index.php
File metadata and controls
71 lines (61 loc) · 2.07 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
<?php /** @noinspection PhpUndefinedMethodInspection */
/**
* Allow cross origin requests (cors)
* replace * with ip/domain to restrict allowed clients
*/
header('Access-Control-Allow-Origin: *');
/**
* Header of allowed HTTP methods
*/
header('Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS');
/**
* Allow content-type header
*/
header('Access-Control-Allow-Headers: Content-Type');
/**
* Autoload libraries and all project classes
*/
require_once 'autoload.php';
use app\App;
use components\http\Route;
use components\http\Request;
use components\http\Response;
use components\http\ResponseException;
$dotEnv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotEnv->load();
App::initialize();
$routes = Route::getInstance();
$pathData = $routes->parseCurrentPath();
try {
if ($pathData->pathExists && $pathData->validMethod) {
$controllerClass = "\\app\\controllers\\" . $pathData->route->controller;
$controller = new $controllerClass;
$request = Request::getInstance();
switch ($request->method()) {
case "POST":
echo $controller->create($request)->execute();
break;
case "GET":
if (isset($pathData->parameter))
$controller->retrieve($pathData->parameter, $request)->execute();
else
$controller->index($request)->execute();
break;
case "PUT":
case "PATCH":
echo $controller->update($pathData->parameter, $request)->execute();
break;
case "DELETE":
echo $controller->destroy($pathData->parameter, $request)->execute();
break;
default:
(new Response([], 200))->execute();
}
} else {
$message = ($pathData->pathExists) ? "Method not allowed." : "Path not found.";
$status = ($pathData->pathExists) ? 405 : 404;
(new Response(["message" => $message], $status))->execute();
}
} catch (ResponseException $e) {
http_response_code(500);
}