PSRs & Middleware Pipeline
This interfaces are already (and amazingly) implemented by Laminas at projects: Diactoros and Stratigility. Siler wraps them and exposes a function-friendly API handling state internally while achieving a fully-featured and declarative way for: Middleware Pipelining.
PSR-7 HTTP Messages
composer require laminas/laminas-diactorosYou can create a superglobals seeded ServerRequest with Siler\Diactoros\request():
use Siler\Diactoros;
$request = Diactoros\request();And create Responses through helpers:
$json = Diactoros\json(['some' => 'data']);
$html = Diactoros\html('<p>some markup</p>');
$text = Diactoros\text('plain text');If none of them fits your needs, you can create a raw Response:
$response = Diactoros\response();
$response->getBody()->write('something');To emit a Response, there is no big deal, if you got Siler, you already imagined that is about one or two function calls, but this time we get the help from HttpHandlerRunner:
composer require laminas/laminas-httphandlerrunnerThen
As in Siler\Http\Response namespace functions, the HttpHandlerRunner\sapi_emit will output headers and text to the buffer, use it carefully.
Example:
PSR-15 Middleware Pipelining
A very simple Hello World example:
It's more uses than actual code because Siler is abstracting all the way down for you.
API
Description
pipe
Creates a Stratigility MiddlewarePipe with a default name and pipes the given Clousure to it already wrapping it inside a MiddlewareInterface decorator, or you can pass any implementation MiddlewareInterface to it.
text
Creates a Diactoros TextResponse. The Diactoros namespace in Siler is basically just helper functions for Responses.
sapi_emit
Creates and immediately calls emit method on a HttpHandlerRunner SapiEmitter.
handle
Calls handle on a MiddlewarePipe marshaling the Request.
request
Creates a Diactoros ServerRequest using PHP's Globals.
Siler's Routes
You can also run pipelines for specific routes:
The second argument on pipe here is a Pipeline name, you can pipe middlewares to any number of pipelines, then in Stratigility\process we marshal it, from the given $request and returns a Closure to be called on a final handler.
Last updated
Was this helpful?