feat: handle redirects

This commit is contained in:
Jamie Barton 2024-07-16 13:03:59 +01:00
parent f96cfaf1db
commit d401b1dbb0
4 changed files with 21 additions and 2 deletions

View File

@ -86,6 +86,12 @@ $app->use(function ($c, $next) {
return $next($c);
});
$app->get("/redirect", function ($c) {
$message = $c->get("message");
return $c->redirect("/greet/hello?name=jamie", 301);
});
$app->get("/", function ($c) {
$message = $c->get("message");

View File

@ -64,4 +64,9 @@ class Context
{
return isset($this->variables[$key]);
}
public function redirect($url, $status = 302)
{
return $this->res->redirect($url, $status);
}
}

View File

@ -90,13 +90,13 @@ class Dumbo
$this->server->sendResponse(
$response->getStatusCode(),
$response->getHeaders(),
$response->getBody()
$response->getBody() ?? ""
);
} elseif ($response !== null) {
$this->server->sendResponse(
200,
["Content-Type" => "text/plain"],
$response
(string) $response
);
} else {
$this->server->sendResponse(204, [], "");

View File

@ -60,4 +60,12 @@ class Response
{
return $this->body;
}
public function redirect($url, $status = 302)
{
$this->statusCode = $status;
$this->header("Location", $url);
return $this;
}
}