Barebone\FastAPI

The Barebone\FastAPI package is a lightweight, attribute-based routing and API specification generator inspired by Python's FastAPI. It allows you to build RESTful APIs and automatically generate interactive Swagger UI documentation directly from PHP 8 attributes declared on your controller methods.

Method Naming Rule: Consistent with the Barebone framework routing, action methods inside your controllers must be entirely lowercase, ending with a suffix specifying the action type (e.g. Action for HTML, JSON for API payloads, AJAX, etc.).
  • App::includeRouter(string $controllerClass): Registers a class-based controller, scanning its public methods for routing attributes.
  • App::run(): Matches the current HTTP request to registered routes, executes the corresponding action, and automatically serves /openapi.json and Swagger UI at /docs.
  • #[Route(string $method, string $path, ?string $summary, ?string $description, ?array $tags)]: Core attribute to define an HTTP route on a method.
  • #[Get], #[Post], #[Put], #[Delete]: Subclasses of Route specialized for respective HTTP verbs.
  • OpenApiGenerator::generate(array $routes, string $title, string $version): Generates an OpenAPI 3.0.0 spec array from the registered routes.

Source Example

<?php

use Barebone\FastAPI\App;
use Barebone\FastAPI\Get;

// 1. Define a Controller with Route Attributes
class MyApiController {
    #[Get('/api/items/{id}', summary: 'Get Item Details', description: 'Retrieve item properties by ID')]
    public function getitemJSON(int $id): array {
        return [
            'id' => $id,
            'name' => 'Demo Item',
            'status' => 'active'
        ];
    }
}

// 2. Setup and Run the FastAPI Application
$app = new App(title: "My Showcase API", version: "1.0.0");
$app->includeRouter(MyApiController::class);
// $app->run(); // Starts dispatching and serves /docs

?>
            

Live Result (OpenAPI Specification Generation)

Below is the generated OpenAPI 3.0 spec generated dynamically from the example route above:

{
    "openapi": "3.0.0",
    "info": {
        "title": "My Showcase API",
        "version": "1.0.0"
    },
    "paths": {
        "\/api\/items\/{id}": {
            "get": {
                "summary": "Get Item Details",
                "description": "Retrieve item properties by ID",
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application\/json": {
                                "schema": {
                                    "type": "object"
                                }
                            }
                        }
                    }
                },
                "tags": [
                    "Showcase"
                ],
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "integer"
                        }
                    }
                ]
            }
        }
    },
    "components": {
        "schemas": []
    }
}            

Class Definition Outline

  • Barebone\FastAPI\App

    Barebone\FastAPI\App
    • [P] $routes : array
    • [P] $controllers : array
    • [P] $title : string
    • [P] $version : string
    • [M] __construct( string $title, string $version ) : void
    • [M] includeRouter( string $controllerClass ) : void
    • [M] run() : void
    • [M] dispatch( array $route, array $pathParams ) : void
    • [M] getParameterTypeName( ReflectionParameter $param ) : string
    • [M] castValue( mixed $value, string $type ) : mixed