2024-07-15 14:27:57 +01:00
|
|
|
# Dumbo
|
|
|
|
|
|
|
|
A lightweight, friendly PHP framework for HTTP — inspired by Hono.
|
|
|
|
|
2024-08-14 20:37:48 +01:00
|
|
|
![Dumbo](/dumbo.jpeg)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
use Dumbo\Dumbo;
|
|
|
|
|
2024-07-16 22:50:59 +01:00
|
|
|
$app = new Dumbo();
|
2024-07-16 12:19:37 +01:00
|
|
|
|
|
|
|
$app->get("/", function ($c) {
|
2024-07-17 14:15:28 +01:00
|
|
|
return $c->json('Hello Dumbo!');
|
2024-07-16 12:19:37 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$app->run();
|
|
|
|
```
|
|
|
|
|
2024-07-17 14:15:28 +01:00
|
|
|
## Routing
|
2024-07-16 12:19:37 +01:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
2024-07-17 14:15:28 +01:00
|
|
|
$app->get('/users', function($c) { /* ... */ });
|
|
|
|
$app->post('/users', function($c) { /* ... */ });
|
|
|
|
$app->put('/users/:id', function($c) { /* ... */ });
|
|
|
|
$app->delete('/users/:id', function($c) { /* ... */ });
|
|
|
|
```
|
2024-07-15 14:27:57 +01:00
|
|
|
|
2024-07-17 14:15:28 +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-07-17 14:15:28 +01:00
|
|
|
$app->get('/users/:id', function($c) {
|
|
|
|
$id = $c->req->param('id');
|
2024-07-15 16:24:38 +01:00
|
|
|
|
2024-07-17 14:15:28 +01:00
|
|
|
return $c->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-07-17 14:15:28 +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-07-17 14:15:28 +01:00
|
|
|
$nestedApp->get('/nested', function($c) {
|
|
|
|
return $c->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-07-17 14:15:28 +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-07-17 14:15:28 +01:00
|
|
|
$app->get('/', function($c) {
|
|
|
|
$pathname = $c->req->pathname();
|
|
|
|
$routePath = $c->req->routePath();
|
|
|
|
$queryParam = $c->req->query('param');
|
|
|
|
$tags = $c->req->queries('tags');
|
|
|
|
$body = $c->req->body();
|
|
|
|
$userAgent = $c->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-07-17 14:15:28 +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-07-17 14:15:28 +01:00
|
|
|
return $c->json(['key' => 'value']);
|
|
|
|
return $c->text('Hello, World!');
|
|
|
|
return $c->html('<h1>Hello, World!</h1>');
|
|
|
|
return $c->redirect('/new-url');
|
|
|
|
```
|
2024-07-15 16:42:42 +01:00
|
|
|
|
2024-07-17 14:15:28 +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-07-17 14:15:28 +01:00
|
|
|
$app->use(function($c, $next) {
|
|
|
|
$response = $next($c);
|
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
|
|
|
|
|
|
|
## Helpers
|
|
|
|
|
|
|
|
### Bearer Auth
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$app = new Dumbo();
|
|
|
|
$protectedRoutes = new Dumbo();
|
|
|
|
|
|
|
|
$token = "mysupersecret";
|
|
|
|
|
|
|
|
$protectedRoutes->use(BearerAuth::bearer($token));
|
|
|
|
|
|
|
|
$protectedRoutes->get("/", function ($c) {
|
|
|
|
return $c->json(["message" => "Welcome to the protected routes!"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->route("/api", $protectedRoutes);
|
|
|
|
```
|