free-elephants / rest-daemon
PHP http socket REST server
Requires
- amphp/aerys: ^0.7.1
- cboden/ratchet: 0.4
- free-elephants/di: ^2.0
- guzzlehttp/psr7: ^1.0
- monolog/monolog: ^1.21
- oscarotero/psr7-middlewares: ^3.18
- psr/container: ^1.0
- psr/log: ^1.0
- relay/middleware: ^1.1
- relay/relay: ~1.0
- symfony/console: ^4.2
- symfony/routing: ^3.1
- zendframework/zend-code: ^3.1
- zendframework/zend-diactoros: ^1.3
- zendframework/zend-http: ^2.5
- zircote/swagger-php: ^2.0
Requires (Dev)
- codeception/codeception: ^2.3
Suggests
- free-elephants/di: Simplest PHP Dependency Injector.
This package is not auto-updated.
Last update: 2024-11-23 21:20:43 UTC
README
Nota Bene: This project uses semver and changelog. But it's not a stable major version. Any minor update (f.e. 0.5.* -> 0.6.*) can break backward compatibility!
Simple PHP7 framework for fast building REST services based on middleware, PSR-7 and react.
Runned instance can be found by link, also see example repo.
Features:
- Middleware oriented request/response handling
- Priority PSR's support: PSR-2, -3, -4, -7, -11, -15 and other.
- Built-in Middleware to support usual REST features, like HTTP based semantics, content types, request parsing, headers.
- Choose one of two available http-daemon drivers: Ratchet ReactPHP or Aerys.
- Swagger Integration
Installation
$ composer require free-elephants/rest-daemon
Usage
See example in example/rest-server.php and documentation.
Create and Run Server:
# your rest-server.php script
$server = new RestServer('127.0.0.1', 8080, '0.0.0.0', ['*']); // <- it's default arguments values
$server->run();
# can be runned as
$ php ./rest-server.php
Add Your RESTful API Endpoints
Any endpoint method handler can be Middleware-like callable implementation: function or class with __invoke() method.
<?php class GetAttributeHandler extends AbstractEndpointMethodHandler { public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $name = $request->getAttribute('name', 'World'); $response->getBody()->write('{ "hello": "' . $name . '!" }'); return $next($request, $response); } } $greetingAttributeEndpoint = new BaseEndpoint('/greeting/{name}', 'Greeting by name in path'); $greetingAttributeEndpoint->setMethodHandler('GET', new GetAttributeHandler()); $server->addEndpoint($greetingAttributeEndpoint);
See how to build server for step by step in one script
RestServerBuilder
You can use php-di (or another PSR-11 container implementation) and routing file configuration with RestServerBuilder for more configuring and coding less.
See example with file based routing and dependencies configuration: rest-server.php
Routing
You can link with every method in route a handler, and optionally organize routes by modules. By default server contain 1 default module for all endpoints. See example: routes.php
Configure Common Application Middleware
By default server instance provide collection with some useful middleware. You can extend or override it:
<?php $requestCounter = function ( ServerRequestInterface $request, ResponseInterface $response, callable $next ) { static $requestNumber = 0; printf('[%s] request number #%d handled' . PHP_EOL, date(DATE_ISO8601), ++$requestNumber); return $next($request, $response); }; $extendedDefaultMiddlewareCollection = new DefaultEndpointMiddlewareCollection([], [$requestCounter]); $server->setMiddlewareCollection($extendedDefaultMiddlewareCollection);
Every endpoint's method handler will be wrapped to this collection and called between defined as after
and before
middleware.
Also you can configure default middleware collection with access to every built-in middleware by key: this collection implements ArrayAccess interface.
<?php $server->getMiddlewareCollection()->getBefore()->offsetUnset(\FreeElephants\RestDaemon\Middleware\MiddlewareRole::NO_CONTENT_STATUS_SETTER);
Customize Endpoint Middleware
... Will be implemented...
Debugging and Logging
... Will be implemented...