PHP code example of notrab / dumbo
1. Go to this page and download the library: Download notrab/dumbo library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
notrab / dumbo example snippets
umbo\Dumbo;
$app = new Dumbo();
$app->use(function ($context, $next) {
$context->set('message', 'Hello from middleware!');
return $next($context);
});
$app->get('/', function ($context) {
return $context->json([
'message' => $context->get('message'),
'timestamp' => time()
]);
});
$app->get('/users/:id', function ($context) {
$id = $context->req->param('id');
return $context->json(['userId' => $id]);
});
$app->post('/users', function ($context) {
$body = $context->req->body();
return $context->json($body, 201);
});
$app->run();
$app->get('/users', function($context) { /* ... */ });
$app->post('/users', function($context) { /* ... */ });
$app->put('/users/:id', function($context) { /* ... */ });
$app->delete('/users/:id', function($context) { /* ... */ });
$app->get('/users/:id', function($context) {
$id = $context->req->param('id');
return $context->json(['id' => $id]);
});
$nestedApp = new Dumbo();
$nestedApp->get('/nested', function($context) {
return $context->text('This is a nested route');
});
$app->route('/prefix', $nestedApp);
$app->get('/', function($context) {
$pathname = $context->req->pathname();
$routePath = $context->req->routePath();
$queryParam = $context->req->query('param');
$tags = $context->req->queries('tags');
$body = $context->req->body();
$userAgent = $context->req->header('User-Agent');
});
return $context->json(['key' => 'value']);
return $context->text('Hello, World!');
return $context->html('<h1>Hello, World!</h1>');
return $context->redirect('/new-url');
$app->use(function($context, $next) {
$response = $next($context);
return $response;
});
$app = new Dumbo();
// Set configuration values
$app->set('DB_URL', 'mysql://user:pass@localhost/mydb');
$app->set('API_KEY', 'your-secret-key');
$app->set('DEBUG', true);
// Get configuration values
$dbUrl = $app->get('DB_URL');
$apiKey = $app->get('API_KEY');
$debug = $app->get('DEBUG');
// Use configuration in your routes
$app->get('/api/data', function(Context $context) {
$apiKey = $context->get('API_KEY');
// Use $apiKey in your logic...
return $context->json(['message' => 'API key is set']);
});
$app->run();