2024-01-15 08:58:24 +01:00
|
|
|
<?php
|
2023-12-06 08:33:25 +01:00
|
|
|
/*
|
|
|
|
Dieses Skript analysiert die URL und generiert daraus die passenden Controller-Aufrufe
|
|
|
|
|
|
|
|
Für die Beispiel-URL
|
|
|
|
|
|
|
|
https://username.web.pb.bib.de/restAPI.php/aufgabe/3
|
|
|
|
|
|
|
|
liefert
|
|
|
|
|
|
|
|
$_SERVER['PATH_INFO'] den URL-Abschnitt "aufgabe/3"
|
|
|
|
|
|
|
|
und
|
|
|
|
|
|
|
|
file_get_contents('php://input') die übergebenen json-Strings (für PUT/POST-Requests)
|
|
|
|
*/
|
2024-01-15 08:58:24 +01:00
|
|
|
spl_autoload_register(function ($className) {
|
|
|
|
if (substr($className, 0, 4) !== 'ppb\\') {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-06 08:33:25 +01:00
|
|
|
|
2024-01-15 08:58:24 +01:00
|
|
|
$fileName = __DIR__ . '/' . str_replace('\\', DIRECTORY_SEPARATOR, substr($className, 4)) . '.php';
|
2023-12-06 08:33:25 +01:00
|
|
|
|
2024-01-15 08:58:24 +01:00
|
|
|
if (file_exists($fileName)) {
|
|
|
|
include $fileName;
|
2023-12-06 08:33:25 +01:00
|
|
|
}
|
2024-01-15 08:58:24 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$endpoint = explode('/', trim($_SERVER['PATH_INFO'], '/'));
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
|
|
|
|
$controllerName = $endpoint[0];
|
|
|
|
$endpoint2 = isset($endpoint[1]) ? $endpoint[1] : false;
|
|
|
|
$id = false;
|
|
|
|
$alias = false;
|
|
|
|
|
|
|
|
if ($endpoint2) {
|
|
|
|
if (preg_match('/^[0-9]+$/', $endpoint2)) {
|
|
|
|
$id = $endpoint2;
|
2023-12-06 08:33:25 +01:00
|
|
|
} else {
|
2024-01-15 08:58:24 +01:00
|
|
|
$alias = $endpoint2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$controllerClassName = 'ppb\\Controller\\' . ucfirst($controllerName) . 'Controller';
|
2023-12-06 08:33:25 +01:00
|
|
|
|
2024-01-15 08:58:24 +01:00
|
|
|
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);
|
2023-12-06 08:33:25 +01:00
|
|
|
}
|
2024-01-15 08:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (method_exists($controllerClassName, $methodName)) {
|
|
|
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
|
|
|
$controller = new $controllerClassName();
|
|
|
|
//GET
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == "GET") {
|
|
|
|
|
|
|
|
echo $controller->$methodName($id);
|
|
|
|
|
|
|
|
} else
|
|
|
|
//POST
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == "POST") {
|
|
|
|
echo $controller->$methodName($data);
|
|
|
|
} else
|
|
|
|
//DELETE
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == "DELETE") {
|
|
|
|
echo $controller->$methodName($id);
|
|
|
|
} else
|
|
|
|
//PUT
|
|
|
|
{
|
|
|
|
echo $controller->$methodName($id, $data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//http_response_code(404);
|
|
|
|
new \ppb\Library\Msg(true, 'Page not found: ' . $controllerClassName . '::' . $methodName);
|
|
|
|
|
|
|
|
}
|
2023-12-06 08:33:25 +01:00
|
|
|
?>
|