2024-07-15 14:27:57 +01:00
# Dumbo
2024-08-29 09:51:28 +01:00
A lightweight, friendly PHP framework for HTTP — inspired by Hono. [Join us on Discord ](https://discord.gg/CYkq2A6G )
2024-07-15 14:27:57 +01:00
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
2024-08-26 20:11:20 +01:00
```bash
curl -H 'Authorization: Bearer mysupersecret' http://localhost:8000/api
```
2024-08-26 19:54:49 +01:00
```php
< ?php
$app = new Dumbo();
$protectedRoutes = new Dumbo();
2024-08-28 10:24:23 +01:00
$protectedRoutes->use(BearerAuth::bearerAuth([
'tokens' => ['token1', 'token2'],
'realm' => 'API Access'
]));
$token = "mysupersecret";
2024-08-26 19:54:49 +01:00
2024-08-27 12:02:24 +06:00
// You can add custom failure message as a second argument.😍 It's optional.
2024-08-28 10:24:23 +01:00
$protectedRoutes->use(BearerAuth::bearerAuth($token, 'Unauthorized request.'));
// Custom token verification function
$app->use(BearerAuth::bearerAuth([
'verifyToken' => function($token, $ctx) {
// Perform custom token verification logic
return verifyJWT($token);
},
'realm' => 'JWT API'
]));
2024-08-27 12:02:24 +06:00
2024-08-26 19:54:49 +01:00
$protectedRoutes->get("/", function ($c) {
return $c->json(["message" => "Welcome to the protected routes!"]);
});
$app->route("/api", $protectedRoutes);
```
2024-08-28 10:08:30 +01:00
### Exception handlers
```php
< ?php
$app = new Dumbo();
$app->post('/', function(Context $c) {
if (!checkAuthStatus()) {
throw new HTTPException(401, 'Unauthorized');
}
});
```
Or with a custom response:
```php
< ?php
$app = new Dumbo();
2024-08-28 10:24:23 +01:00
$app->onError(function (\Exception $error, Context $c) {
// Custom error response
if ($error instanceof HTTPException) {
// We can now use the toArray() method to get a structured error response
return $c->json($error->toArray(), $error->getStatusCode());
}
// Gotta catch 'em all
return $c->json(['error' => 'Internal Server Error'], 500);
});
2024-08-28 10:08:30 +01:00
$app->post('/', function(Context $c) {
if (!doSomething()) {
$customResponse = $c->html('< h1 > Something went wrong< / h1 > ', 404);
2024-08-28 10:24:23 +01:00
throw new HTTPException(
404,
'Something went wrong',
'OPERATION_FAILED',
['operation' => 'doSomething'],
$customResponse
);
2024-08-28 10:08:30 +01:00
}
});
```
2024-08-28 19:29:13 +06:00
### Basic Auth
Implementing Basic authentication on Cloudflare Workers or other platforms can be complex. This helper middleware offers a straightforward solution for securing specific routes.
```php
< ?php
use Dumbo\Dumbo;
use Dumbo\Context;
use Dumbo\Helpers\BasicAuth;
$app = new Dumbo();
// Use case 1: Static username and password
2024-08-29 11:00:25 +06:00
$app->use(BasicAuth::basicAuth("user:password"));
2024-08-28 19:29:13 +06:00
2024-08-29 11:00:25 +06:00
// Use case 2: Dynamic verification
2024-08-28 19:29:13 +06:00
$app->use(BasicAuth::basicAuth([
2024-08-29 11:00:25 +06:00
"verifyUser" => function ($username, $password, $ctx) {
// You could call a database here...
$validUsers = [
"admin" => "strongpassword",
"user" => "password",
];
return isset($validUsers[$username]) & &
$validUsers[$username] === $password;
},
"realm" => "Admin Area",
])
);
// Use case 3: For multiple users
2024-08-28 19:29:13 +06:00
$app->use(BasicAuth::basicAuth([
2024-08-29 11:00:25 +06:00
// You could call a database here...
"users" => [
["username" => "user1", "password" => "pass1"],
["username" => "user2", "password" => "pass2"],
2024-08-28 19:29:13 +06:00
],
2024-08-29 11:00:25 +06:00
"realm" => "Admin Area"
])
);
2024-08-28 19:29:13 +06:00
2024-08-29 11:00:25 +06:00
// Define routes for the above three use cases
2024-08-28 19:29:13 +06:00
$app->get("/", function (Context $ctx) {
return $ctx->json([
"status" => 'success',
2024-08-29 11:00:25 +06:00
"message" => "Your authentication is successful!!!"
2024-08-28 19:29:13 +06:00
], 200);
});
2024-08-29 11:00:25 +06:00
// Use case 4: For nested routes
$api = new Dumbo();
$api->use(BasicAuth::basicAuth("user:password"));
$api->get("/", function (Context $c) {
return $c->html("< h1 > Your authentication is successful! 😎< / h1 > ");
});
$app->route("/api", $api);
2024-08-28 19:29:13 +06:00
$app->run();
```