2024-09-15 10:00:44 +01:00
< p align = "center" >
< img src = "dumbo.jpeg" alt = "Dumbo" width = "600" / >
< h1 align = "center" > Dumbo< / h1 >
< / p >
< p align = "center" >
A lightweight, friendly PHP framework for HTTP — Inspired by Hono.
< / p >
< p align = "center" >
< a href = "https://discord.gg/nAq2h9BfsU" >
2024-10-19 11:24:12 +01:00
< picture >
< img src = "https://img.shields.io/discord/1278637768918171709?color=8A2BE2" alt = "Discord" / >
< / picture >
2024-09-15 10:00:44 +01:00
< / a >
2024-10-19 11:24:12 +01:00
< picture >
< img src = "https://img.shields.io/github/contributors/notrab/dumbo?color=8A2BE2" alt = "Contributors" / >
< / picture >
< a href = "https://packagist.org/packages/notrab/dumbo" >
< picture >
< img src = "https://img.shields.io/packagist/dt/notrab/dumbo?color=8A2BE2" alt = "Total downloads" / >
< / picture >
2024-09-15 10:00:44 +01:00
< / a >
< a href = "/examples" >
< img src = "https://img.shields.io/badge/browse-examples-8A2BE2" alt = "Examples" / >
< / a >
< / p >
2024-08-14 20:37:48 +01:00
2024-10-19 11:24:12 +01:00
## Features
- 🚀 Lightweight and fast
- 🧩 Middleware support
- 🛣️ Flexible routing with parameters
- 🔒 Built-in security features (CSRF, JWT)
- 🍪 Cookie management
- 📅 Date helpers
- 🔍 Request ID for tracing
- 📁 Static file serving
- 🔐 Basic and Bearer authentication
- 📝 Logging support
- 🗃️ HTTP caching
- 🔄 CORS support
- 🧬 Environment-based configuration
2024-07-15 14:27:57 +01:00
## Install
```bash
2024-07-15 14:28:28 +01:00
composer require notrab/dumbo
2024-07-15 14:27:57 +01:00
```
## Quickstart
2024-07-17 14:15:28 +01:00
Here's a basic example of how it works!
2024-07-15 14:27:57 +01:00
```php
< ?php
2024-10-19 11:24:12 +01:00
require __DIR__ . '/vendor/autoload.php';
2024-07-15 14:27:57 +01:00
use Dumbo\Dumbo;
2024-07-16 22:50:59 +01:00
$app = new Dumbo();
2024-07-16 12:19:37 +01:00
2024-10-19 11:24:12 +01:00
$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);
2024-07-16 12:19:37 +01:00
});
$app->run();
```
2024-09-05 10:04:59 +01:00
See the [examples ](/examples ) directory for more quickstarts.
2024-10-19 11:24:12 +01:00
## License
Dumbo is open-sourced software licensed under the [MIT license ](LICENSE ).
## Contributors
![Contributors ](https://contrib.nn.ci/api?repo=notrab/dumbo )
< a href = "https://github.com/notrab/dumbo/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22" >
< picture >
2024-10-19 11:28:05 +01:00
< img src = "https://img.shields.io/github/issues-search/notrab/dumbo?query=is%3Aopen%20is%3Aissue%20label%3A%22good%20first%20issue%22%20&color=8A2BE2" alt = "good first issue" / >
2024-10-19 11:24:12 +01:00
< / picture >
< / a >
## Documentation
### Routing
2024-07-16 12:19:37 +01:00
```php
< ?php
2024-08-29 16:09:38 +01:00
$app->get('/users', function($context) { /* ... */ });
$app->post('/users', function($context) { /* ... */ });
$app->put('/users/:id', function($context) { /* ... */ });
$app->delete('/users/:id', function($context) { /* ... */ });
2024-07-17 14:15:28 +01:00
```
2024-07-15 14:27:57 +01:00
2024-10-19 11:24:12 +01:00
#### Params
2024-07-15 16:24:38 +01:00
2024-07-17 14:15:28 +01:00
```php
< ?php
2024-07-15 19:11:45 +01:00
2024-08-29 16:09:38 +01:00
$app->get('/users/:id', function($context) {
2024-08-29 13:08:21 +01:00
$id = $context->req->param('id');
2024-07-15 16:24:38 +01:00
2024-08-29 13:08:21 +01:00
return $context->json(['id' => $id]);
2024-07-15 14:27:57 +01:00
});
2024-07-17 14:15:28 +01:00
```
2024-07-15 14:27:57 +01:00
2024-10-19 11:24:12 +01:00
#### Nested
2024-07-15 19:11:45 +01:00
2024-07-17 14:15:28 +01:00
```php
< ?php
2024-07-15 19:11:45 +01:00
2024-07-17 14:15:28 +01:00
$nestedApp = new Dumbo();
2024-07-15 19:11:45 +01:00
2024-08-29 16:09:38 +01:00
$nestedApp->get('/nested', function($context) {
2024-08-29 13:08:21 +01:00
return $context->text('This is a nested route');
2024-07-15 19:11:45 +01:00
});
2024-07-17 14:15:28 +01:00
$app->route('/prefix', $nestedApp);
2024-07-15 21:09:07 +01:00
2024-07-17 14:15:28 +01:00
```
2024-07-15 19:11:45 +01:00
2024-10-19 11:24:12 +01:00
### Context
2024-07-15 14:27:57 +01:00
2024-07-17 14:15:28 +01:00
```php
< ?php
2024-07-15 16:24:38 +01:00
2024-08-29 16:09:38 +01:00
$app->get('/', function($context) {
2024-08-29 13:08:21 +01:00
$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');
2024-07-15 14:27:57 +01:00
});
2024-07-17 14:15:28 +01:00
```
2024-07-15 14:27:57 +01:00
2024-08-30 10:25:52 +01:00
### Response
2024-07-16 22:50:59 +01:00
2024-07-17 14:15:28 +01:00
```php
< ?php
2024-07-15 16:42:42 +01:00
2024-08-29 13:08:21 +01:00
return $context->json(['key' => 'value']);
return $context->text('Hello, World!');
return $context->html('< h1 > Hello, World!< / h1 > ');
return $context->redirect('/new-url');
2024-07-17 14:15:28 +01:00
```
2024-07-15 16:42:42 +01:00
2024-08-30 10:25:52 +01:00
### Middleware
2024-07-16 22:50:59 +01:00
2024-07-17 14:15:28 +01:00
```php
< ?php
2024-07-16 22:50:59 +01:00
2024-08-29 16:09:38 +01:00
$app->use(function($context, $next) {
$response = $next($context);
2024-07-16 22:50:59 +01:00
2024-07-17 14:15:28 +01:00
return $response;
2024-07-15 19:11:45 +01:00
});
2024-07-15 14:27:57 +01:00
```
2024-08-26 19:54:49 +01:00
2024-08-30 10:25:52 +01:00
### Custom context
```php
< ?php
$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
2024-09-05 10:04:59 +01:00
$app->get('/api/data', function(Context $context) {
$apiKey = $context->get('API_KEY');
2024-08-30 10:25:52 +01:00
// Use $apiKey in your logic...
2024-09-05 10:04:59 +01:00
return $context->json(['message' => 'API key is set']);
2024-08-29 13:06:57 +01:00
});
2024-08-28 19:29:13 +06:00
$app->run();
```