Share data using Custom Context
mainYou can share values across middleware and handlers using $context->set() and $context->get().
- To apply middleware globally, use
$app->use(callback). - To apply middleware only to a specific path prefix, use
$app->use('/prefix', callback).
<?php
$app = new Dumbo();
// Available to every route
$app->use(function ($context, $next) {
$context->set('DB_URL', 'mysql://user:pass@localhost/mydb');
$context->set('API_KEY', 'your-secret-key');
return $next($context);
});
// Or only to routes under a given prefix
$app->use('/api', function ($context, $next) {
$context->set('DEBUG', true);
return $next($context);
});
$app->get('/api/data', function ($context) {
$apiKey = $context->get('API_KEY');
// Use $apiKey in your logic...
return $context->json(['message' => 'API key is set']);
});
$app->run();