Search by

byjg / restserver

byjg

Create RESTFull services with different and customizable output handlers (JSON, XML, Html, etc.). Auto-Generate routes from swagger.json definition.

Package info

github.com/byjg/php-restserver

pkg:composer/byjg/restserver

Statistics

Installs: 53 876

Dependents: 6

Suggesters: 0

Stars: 4

Open Issues: 0

6.0.0 2025-11-24 18:01 UTC

README

sidebar_key restserver
sidebar_category http-apis
tags
php http

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