byjg / restserver
Create RESTFull services with different and customizable output handlers (JSON, XML, Html, etc.). Auto-Generate routes from swagger.json definition.
Requires
- php: >=8.3 <8.6
- ext-json: *
- byjg/cache-engine: ^6.0
- byjg/jwt-wrapper: ^6.0
- byjg/serializer: ^6.0
- byjg/singleton-pattern: ^6.0
- byjg/webrequest: ^6.0
- nikic/fast-route: v1.3.*
- psr/http-client: ^1.0
- psr/http-factory: ^1.0|^1.1
- psr/log: ^1.0|^2.0|^3.0
Requires (Dev)
- monolog/monolog: ^2.9|^3.4
- phpunit/phpunit: ^10.5|^11.5
- vimeo/psalm: ^5.9|^6.13
Suggests
- ext-fileinfo: *
Provides
None
Conflicts
None
Replaces
None
README
| sidebar_key | restserver | ||
|---|---|---|---|
| sidebar_category | http-apis | ||
| tags |
|
Rest Server
A REST server whose routes can come straight from your OpenAPI specification: document the API once, and that document is the router.
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:
- Defining Route Names
- Create Routes using Closures
- Create Routes Manually
- Create Routes using PHP Attributes
- Auto-Generate from an OpenApi definition (hot)
Processing the request and output the response:
Advanced:
- Middleware
- Error Handler
- Dependency Injection
- Intercepting the Request
- Output Processors
- Caching Routes
Additional topics:
- Mock Testing
- Route Metadata
- Content Negotiation
- Custom HTTP Status Codes
- CSV Endpoint Example
- PSR-7 Adapters
Installation
composer require "byjg/restserver"
Running Tests
docker compose up -d vendor/bin/phpunit