2024-07-15 14:27:57 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require "vendor/autoload.php";
|
|
|
|
|
|
|
|
use Dumbo\Dumbo;
|
|
|
|
|
|
|
|
$app = new Dumbo();
|
2024-07-15 16:18:47 +01:00
|
|
|
$user = new Dumbo();
|
|
|
|
|
|
|
|
$userData = [
|
|
|
|
"id" => 1,
|
|
|
|
"name" => "Jamie Barton",
|
|
|
|
"email" => "jamie@notrab.dev",
|
|
|
|
];
|
|
|
|
|
2024-07-15 16:24:38 +01:00
|
|
|
$user->get("/", function ($c) use ($userData) {
|
|
|
|
return $c->json($userData);
|
2024-07-15 16:18:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$user->get("/:id", function ($c) use ($userData) {
|
2024-07-15 18:59:04 +01:00
|
|
|
$id = (int) $c->req->param("id");
|
2024-07-15 16:18:47 +01:00
|
|
|
|
|
|
|
if ($id !== $userData["id"]) {
|
|
|
|
return $c->json(["error" => "User not found"], 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c->json($userData);
|
|
|
|
});
|
2024-07-15 14:27:57 +01:00
|
|
|
|
|
|
|
$app->get("/", function ($c) {
|
2024-07-15 16:18:47 +01:00
|
|
|
return $c->html("<h1>Hello from Dumbo!</h1>", 200, [
|
|
|
|
"X-Hello" => "World",
|
|
|
|
]);
|
2024-07-15 14:27:57 +01:00
|
|
|
});
|
|
|
|
|
2024-07-15 18:59:04 +01:00
|
|
|
$app->get("/greet/:greeting", function ($c) {
|
|
|
|
$greeting = $c->req->param("greeting");
|
|
|
|
$name = $c->req->query("name");
|
2024-07-15 16:18:47 +01:00
|
|
|
|
2024-07-15 14:27:57 +01:00
|
|
|
return $c->json([
|
|
|
|
"message" => "$greeting, $name!",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2024-07-15 16:18:47 +01:00
|
|
|
$app->route("/users", $user);
|
|
|
|
|
2024-07-15 16:42:42 +01:00
|
|
|
$app->use(function ($c, $next) {
|
2024-07-15 18:59:04 +01:00
|
|
|
$c->header("X-Powered-By", "Dumbo");
|
2024-07-15 16:42:42 +01:00
|
|
|
|
|
|
|
return $next($c);
|
|
|
|
});
|
|
|
|
|
2024-07-15 14:27:57 +01:00
|
|
|
$app->run();
|