Imam Ali Mustofa c1d8347f69
fix: app config setget (#39)
* refactor: add missing response interface

* fix: app config setter and getter

* Apply suggestions from code review

Co-authored-by: Jamie Barton <jamie@notrab.dev>

* feat: introduce HasConfig trait

* refactor: remove duplicate methods and use HasConfig trait

* changes

* revert trait for now

* add back variables

* revert examples demo

---------

Co-authored-by: Jamie Barton <jamie@notrab.dev>
2024-09-05 09:26:51 +01:00

45 lines
960 B
PHP

<?php
require "vendor/autoload.php";
use Dumbo\Dumbo;
$app = new Dumbo();
// Context middleware for all routes
$app->use(function ($context, $next) {
$context->set("message", "Hello Dumbo!");
return $next($context);
});
$app->get("/", function ($context) {
$message = $context->get("message");
return $context->json([
"message" => $message,
]);
});
// Route specific context via middleware
$app->use("/api", function ($context, $next) {
$context->set("databaseUrl", "something-connection-uri");
$context->set("authToken", "my-secret-auth-token");
return $next($context);
});
$app->get("/api", function ($context) {
$databaseUrl = $context->get("databaseUrl");
$authToken = $context->get("authToken");
$message = $context->get("message");
return $context->json([
"message" => $message,
"databaseUrl" => $databaseUrl,
"authToken" => $authToken,
]);
});
$app->run();