Joseph Ajibodu 8b2413b84b
Update .gitattributes and .gitignore for improved package and repo management (#18)
* exclude non-essential files and directories from package distribution

- Added entries to .gitattributes to exclude:
  - /examples: Example files
  - /tests: Test files
  - .gitattributes: Git-specific configuration
  - .github: GitHub-related files
  - .gitignore: Git ignore rules
  - CHANGELOG-*: Changelog files
  - CODE_OF_CONDUCT.md: Code of conduct document
  - CONTRIBUTING.md: Contribution guidelines
  - UPGRADING.md: Upgrade instructions
  - RELEASE.md: Release notes

* update .gitignore to include additional files and directories

- Added .idea/ and .idea: Exclude JetBrains IDE configuration directories
- Added composer.lock: Exclude Composer lock file (if needed)
- Added .fleet: Exclude Fleet IDE configuration (if applicable)
- Added .vscode: Exclude Visual Studio Code configuration directory

---------

Co-authored-by: Jamie Barton <jamie@notrab.dev>
2024-08-28 14:17:06 +01:00
2024-08-28 13:47:59 +01:00
2024-08-28 13:47:59 +01:00

Dumbo

A lightweight, friendly PHP framework for HTTP — inspired by Hono.

Dumbo

Install

composer require notrab/dumbo

Quickstart

Here's a basic example of how it works!

<?php

use Dumbo\Dumbo;

$app = new Dumbo();

$app->get("/", function ($c) {
    return $c->json('Hello Dumbo!');
});

$app->run();

Routing

<?php

$app->get('/users', function($c) { /* ... */ });
$app->post('/users', function($c) { /* ... */ });
$app->put('/users/:id', function($c) { /* ... */ });
$app->delete('/users/:id', function($c) { /* ... */ });

Params

<?php

$app->get('/users/:id', function($c) {
    $id = $c->req->param('id');

    return $c->json(['id' => $id]);
});

Nested

<?php

$nestedApp = new Dumbo();

$nestedApp->get('/nested', function($c) {
    return $c->text('This is a nested route');
});

$app->route('/prefix', $nestedApp);

Context

<?php

$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');
});

Response

<?php

return $c->json(['key' => 'value']);
return $c->text('Hello, World!');
return $c->html('<h1>Hello, World!</h1>');
return $c->redirect('/new-url');

Middleware

<?php

$app->use(function($c, $next) {
    $response = $next($c);

    return $response;
});

Helpers

Bearer Auth

curl -H 'Authorization: Bearer mysupersecret' http://localhost:8000/api
<?php

$app = new Dumbo();
$protectedRoutes = new Dumbo();


$protectedRoutes->use(BearerAuth::bearerAuth([
    'tokens' => ['token1', 'token2'],
    'realm' => 'API Access'
]));

$token = "mysupersecret";

// You can add custom failure message as a second argument.😍 It's optional.
$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'
]));

$protectedRoutes->get("/", function ($c) {
    return $c->json(["message" => "Welcome to the protected routes!"]);
});

$app->route("/api", $protectedRoutes);

Exception handlers

<?php

$app = new Dumbo();

$app->post('/', function(Context $c) {
    if (!checkAuthStatus()) {
        throw new HTTPException(401, 'Unauthorized');
    }
});

Or with a custom response:

<?php

$app = new Dumbo();

$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);
});

$app->post('/', function(Context $c) {
    if (!doSomething()) {
        $customResponse = $c->html('<h1>Something went wrong</h1>', 404);
        throw new HTTPException(
            404,
            'Something went wrong',
            'OPERATION_FAILED',
            ['operation' => 'doSomething'],
            $customResponse
        );
    }
});
Description
A lightweight, friendly PHP framework for HTTP.
Readme MIT 824 KiB
Languages
PHP 100%