Dumbo PHP Framework

repository·main·Indexed 43 days ago

https://github.com/notrab/dumbo

A lightweight, fast, and friendly PHP framework for HTTP inspired by the Hono framework. It provides essential features including routing, middleware, and security out of the box.

Tokens
20.3K
Snippets
93
Records
100
Agent score
54%

What's inside Dumbo

  1. Share data using Custom Context

    main

    You can share values across middleware and handlers using $context->set() and $context->get().

    • To apply middleware globally, use $app->use(callback).
    • To apply middleware only to a specific path prefix, use $app->use('/prefix', callback).
    <?php
    
    $app = new Dumbo();
    
    // Available to every route
    $app->use(function ($context, $next) {
        $context->set('DB_URL', 'mysql://user:pass@localhost/mydb');
        $context->set('API_KEY', 'your-secret-key');
    
        return $next($context);
    });
    
    // Or only to routes under a given prefix
    $app->use('/api', function ($context, $next) {
        $context->set('DEBUG', true);
    
        return $next($context);
    });
    
    $app->get('/api/data', function ($context) {
        $apiKey = $context->get('API_KEY');
    
        // Use $apiKey in your logic...
        return $context->json(['message' => 'API key is set']);
    });
    
    $app->run();
  2. Quickstart with Dumbo

    main

    Dumbo is a lightweight HTTP framework. You can initialize an application instance, register middleware, define routes (GET, POST, etc.), and run the application. The $context object is passed to every handler and provides access to the request and response utilities.

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use Dumbo//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();
  3. Run the JWT Auth Example

    main

    To run the JWT authentication demonstration, follow these steps to install dependencies, start the local server, and test the authentication flow using curl.

    # 1. Install dependencies
    composer install
    
    # 2. Start the server
    composer start
    
    # 3. Access the public route
    curl http://localhost:8000
    
    # 4. Try to access the protected route (should fail)
    curl http://localhost:8000/protected
    
    # 5. Login to receive a JWT cookie
    curl -X POST -H "Content-Type: application/json" -d '{"username":"user1","password":"password1"}' http://localhost:8000/login -c cookies.txt
    
    # 6. Access the protected route using the stored cookie
    curl http://localhost:8000/protected -b cookies.txt
    
    # 7. Logout
    curl -X POST http://localhost:8000/logout -b cookies.txt
  4. Run the Latte Templating Example

    main

    This example demonstrates how to integrate the Latte templating engine with Dumbo to render views and serve them as HTML pages. To run the example locally, follow these steps:

    1. Install Dependencies: Use Composer to install the required PHP packages.
    2. Start the Server: Use the Composer script to launch the development server.
    composer install
    composer start
  5. Run the Body Limit Example

    main

    To run the demonstration of the body limit middleware, follow these steps:

    1. Install the necessary PHP dependencies using Composer:
      composer install
    2. Start the local development server:
      composer start
    3. Test the body limit by sending POST requests to the /upload route. Files smaller than 1MB will succeed, while files larger than 1MB will trigger an error.
    composer install
    composer start
  6. Run the Dumbo Send Example

    main

    To run the demonstration of returning different response types (SVG, JPEG, README, CSV, XML) using Dumbo, follow these steps:

    1. Install the necessary PHP dependencies using Composer.
    2. Start the local development server.
    3. Use curl to access the predefined routes and verify the response types.
    # 1. Install dependencies
    composer install
    
    # 2. Start the server
    composer start
    
    # 3. Access routes
    curl -i http://localhost:8000/logo > logo.svg
    curl -i http://localhost:8000/image > image.jpeg
    curl -i http://localhost:8000/readme
    curl -i http://localhost:8000/export > users.csv
    curl -i http://localhost:8000/feed
  7. Run the Bearer Auth Nested Route Example

    main

    To run this specific demonstration of bearer authentication applied to nested routes (such as /api), follow these steps:

    1. Install the required PHP dependencies using Composer.
    2. Start the local development server.
    3. Test the protected route using curl, ensuring you include the Authorization: Bearer <token> header.
    # 1. Install dependencies
    composer install
    
    # 2. Start the server
    composer start
    
    # 3. Access the protected route
    curl -H "Authorization: Bearer mysupersecret" http://localhost:8000/api
  8. Run the Logger Example

    main

    To run the logger demonstration, follow these steps to install dependencies, start the local development server, and trigger the example route using curl.

    # 1. Install dependencies
    composer install
    
    # 2. Start the server
    composer start
    
    # 3. Access the route
    curl -v http://localhost:8000
  9. Run the Context View Blade example

    main

    To run the demonstration of passing a render closure to context and generating a view using the Latte template engine, follow these steps:

    1. Install the required PHP dependencies using Composer.
    2. Start the local development server.
    3. Access the demo via a web browser or curl at http://localhost:8000.
    # Install dependencies
    composer install
    
    # Start the server
    composer start
    
    # Access the demo
    curl http://localhost:8000
  10. Run the Context and Latte View Example

    main

    This example demonstrates how to pass a render closure to the Dumbo context to generate a view using the Latte template engine. To run the demonstration locally, follow these steps:

    1. Install the necessary PHP dependencies using Composer.
    2. Start the built-in development server.
    3. Access the demo route via curl or a web browser.
    # 1. Install dependencies
    composer install
    
    # 2. Start the server
    composer start
    
    # 3. Access the demo route
    curl http://localhost:8000
  11. Run the Route Params Example

    main

    To run the route parameters demonstration, install the project dependencies via Composer and then start the built-in development server. Once running, you can test various routing scenarios using curl.

    # 1. Install dependencies
    composer install
    
    # 2. Start the server
    composer start
  12. Run the Context Variables Example

    main

    To run the demonstration of getting and setting custom variables on the root and route middleware, follow these steps:

    1. Install the required PHP dependencies using Composer.
    2. Start the local development server.
    3. Test the routes using curl to observe how context variables are handled.
    # 1. Install dependencies
    composer install
    
    # 2. Start the server
    composer start
    
    # 3. Access the routes
    curl http://localhost:8000
    curl http://localhost:8000/api