This package include the following Relay-compatible response middleware:
- ResponseSender to send a PSR-7 response
- ExceptionHandler to handle exceptions from subsequent middleware
- FormContentHandler to deserialize the URL-encoded payload of a PSR-7 request
- JsonContentHandler to deserialize the JSON payload of a PSR-7 request
- JsonDecoder to deserialize the JSON payload of a PSR-7 request (deprecated)
This package is installable and PSR-4 autoloadable via Composer as relay/middleware
.
The ResponseSender does just what it sounds like: it sends the PSR-7 response object.
The ResponseSender does nothing with the $request
or $response
, passing them immediately to $next
. Afterwards, it takes the returned $response
and sends it using header()
and echo
, and returns the sent $response
.
The ResponseSender is intended to go at the top of the Relay queue, so that it is the middleware with the last opportunity to do something with the returned response.
To add the ResponseSender to your Relay queue, instantiate it directly ...
$queue[] = new \Relay\Middleware\ResponseSender();
... or use a $resolver
of your choice to instantiate it from the $queue
.
Similarly, the ExceptionHandler does what it sound like: it catches any exceptions that bubble up through the subsequent middleware decorators.
The ExceptionHandler does nothing with the $request
or $response
, and passes them directly to $next
inside a try/catch
block. If no exception bubbles up, it returns the $response
from $next
. However, if it catches an exception, it returns an entirely new $response
object with the exception message and an HTTP 500 status code. It then returns the new $response
object.
The ExceptionHandler is intended to go near the top of the Relay queue, but after the ResponseSender, so that the ResponseSender can then send the returned $response
.
To add the ExceptionHandler to your queue, instantiate it directly with an empty $response implementation object ...
$queue = new \Relay\Middleware\ExceptionHandler(new ResponseImplementation());
... or use a $resolver
of your choice to instantiate it from the $queue
.
Again, the JsonContentHandler does what it sounds like: it deserializes the JSON payload of a PSR-7 request object and makes the parameters available in subsequent middleware decorators.
The JsonContentHandler checks the incoming request for a method other than GET
and for an application/json
or application/vnd.api+json
Content-Type
header.
If it finds both of these, it parses the JSON and makes it available as the
parsed body of the $request
before passing it and the $response
to $next
.
If the method is GET
or the Content-Type
header defines a different mime type,
the JsonContentHandler ignores the $request
and continues the chain.
To add the JsonContentHandler to your queue, instantiate it directly...
$queue = new \Relay\Middleware\JsonContentHandler();
... or use a $resolver
of your choice to instantiate it from the $queue
.
To access the decoded parameters in subsequent middleware, use the
getParsedBody()
method of the $request
$decodedJsonData = $request->getParsedBody();
FormContentHandler works almost identically to JsonContentHandler, but parses
payloads of requests that have application/x-www-form-urlencoded
as the Content-Type
.
NOTE: This handler has been deprecated in favor of JsonContentHandler!
Again, the JsonDecoder does what it sounds like: it deserializes the JSON payload of a PSR-7 request object and makes the parameters available in subsequent middleware decorators.
The JsonDecoder checks the incoming request for a method other than GET
and
for an application/json
Content-Type
header. If it finds both of these, it
decodes the JSON and makes it available as the parsed body of the $request
before passing it and the $response
to $next
. If the method is GET
or the
Content-Type
header does not specify application/json
, the JsonDecoder
does nothing with the $request
and passes it and the $response
to $next
.
To add the JsonDecoder to your queue, instantiate it directly...
$queue = new \Relay\Middleware\JsonDecoder();
... or use a $resolver
of your choice to instantiate it from the $queue
.
To access the decoded parameters in subsequent middleware, use the
getParsedBody()
method of the $request
$decodedJsonData = $request->getParsedBody();