Pular para o conteúdo principal

Rest Server

A REST server whose routes can come straight from your OpenAPI specification: document the API once, and that document is the router.

Sponsor Build Status Opensource ByJG Install MCP Server GitHub source GitHub license GitHub release

Why​

In most frameworks the routes and the API documentation are two separate things, written twice and kept in sync by hand. Sooner or later they disagree, and the documentation is the one that is wrong.

RestServer can read the routes from the OpenAPI specification itself. Each operationId names the method that handles it:

$restServer = new Server();
$restServer->handle(new OpenApiRouteList(__DIR__ . '/openapi.json'));
  • The specification is the router. An endpoint that is not documented does not exist, so the documentation cannot drift from the code. Generate the specification from attributes with swagger-php, or write it by hand.
  • Write objects, not responses. A controller writes data to an HttpResponse; the output handler serializes it to JSON, XML, CSV or HTML, chosen by content negotiation.
  • Routes the usual ways too: PHP attributes, closures, or a list built by hand.

Documentation​

Setup:

Creating and customizing routes:

Processing the request and output the response:

Advanced:

Additional topics:

Installation​

composer require "byjg/restserver"

Running Tests​

docker compose up -d
vendor/bin/phpunit

Open source ByJG