Add initial REST API structure with starter files

This commit is contained in:
MosLaptop\Not.Reda 2025-06-13 13:10:19 +02:00
parent 2c794a113a
commit ea58af4fcc
2 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,5 @@
# Netscape HTTP Cookie File
# https://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.
localhost FALSE / FALSE 0 PHPSESSID f8kg99kbocuuiv0u72sccru241

63
restAPI.php Normal file
View File

@ -0,0 +1,63 @@
<?php
session_start();
spl_autoload_register(function ($className) {
if (substr($className, 0, 4) !== 'ppb\\') { return; }
$fileName = __DIR__.'/'.str_replace('\\', DIRECTORY_SEPARATOR, substr($className, 4)).'.php';
if (file_exists($fileName)) { include $fileName; }
});
$endpoint = explode('/', trim($_SERVER['PATH_INFO'],'/'));
$data = json_decode(file_get_contents('php://input'), true);
$controllerName = $endpoint[0];
$id = false;
$alias = false;
if (isset($endpoint[1])) {
if (preg_match('/\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b/', $endpoint2)) {
$id = $endpoint2;
} else {
$alias = $endpoint2;
}
}
$controllerClassName = 'ppb\\Controller\\'.ucfirst($controllerName). 'Controller';
if ($_SERVER['REQUEST_METHOD'] == "DELETE") {
$methodName = "delete" . ucfirst($controllerName);
} else if ($_SERVER['REQUEST_METHOD'] == "PUT") {
$methodName = "update" . ucfirst($controllerName);
} else if ($_SERVER['REQUEST_METHOD'] == "POST") {
$methodName = "write" . ucfirst($controllerName);
} else if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($alias) {
$methodName = $alias;
} else {
$methodName = "get" . ucfirst($controllerName);
}
}
if (method_exists($controllerClassName, $methodName)) {
$controller = new $controllerClassName();
if ($_SERVER['REQUEST_METHOD'] == "GET") {
if ($id) {
$controller->$methodName($id);
} else {
$controller->$methodName();
}
} else if ($_SERVER['REQUEST_METHOD'] == "POST"){
$controller->$methodName($data);
} else if ($_SERVER['REQUEST_METHOD'] == "DELETE"){
$controller->$methodName($id);
} else {
$controller->$methodName($id, $data);
}
} else {
//http_response_code(404);
new \ppb\Library\Msg(true, 'Page not found: '.$controllerClassName.'::'.$methodName);
}
?>