mirror of
https://github.com/notrab/dumbo.git
synced 2025-01-17 22:28:25 +01:00
c1d8347f69
* 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>
45 lines
960 B
PHP
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();
|