mirror of
https://github.com/notrab/dumbo.git
synced 2025-01-29 03:37:38 +01:00
feat: context variables
This commit is contained in:
parent
219ae52bf3
commit
f96cfaf1db
@ -75,6 +75,11 @@ $app->get("/greet/:greeting", function ($c) {
|
||||
|
||||
$app->route("/users", $user);
|
||||
|
||||
$app->use(function ($ctx, $next) {
|
||||
$ctx->set("message", "Dumbo");
|
||||
return $next($ctx);
|
||||
});
|
||||
|
||||
$app->use(function ($c, $next) {
|
||||
$c->header("X-Powered-By", "Dumbo");
|
||||
|
||||
@ -82,7 +87,9 @@ $app->use(function ($c, $next) {
|
||||
});
|
||||
|
||||
$app->get("/", function ($c) {
|
||||
return $c->html("<h1>Hello from Dumbo!</h1>", 200, [
|
||||
$message = $c->get("message");
|
||||
|
||||
return $c->html("<h1>Hello from $message!</h1>", 200, [
|
||||
"X-Hello" => "World",
|
||||
]);
|
||||
});
|
||||
|
@ -5,6 +5,7 @@ class Context
|
||||
{
|
||||
public $req;
|
||||
public $res;
|
||||
private $variables;
|
||||
|
||||
public function __construct($method, $params, $query, $body, $headers)
|
||||
{
|
||||
@ -48,4 +49,19 @@ class Context
|
||||
{
|
||||
return $this->req->method();
|
||||
}
|
||||
|
||||
public function set(string $key, $value): void
|
||||
{
|
||||
$this->variables[$key] = $value;
|
||||
}
|
||||
|
||||
public function get(string $key)
|
||||
{
|
||||
return $this->variables[$key] ?? null;
|
||||
}
|
||||
|
||||
public function has(string $key): bool
|
||||
{
|
||||
return isset($this->variables[$key]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user