-
Notifications
You must be signed in to change notification settings - Fork 2
Middleware: process Example
Terry L edited this page Jun 21, 2020
·
3 revisions
Shieldon\Psr15\Middleware
-
param
ServerRequestInterface
request*
The server request. -
param
RequestHandlerInterface
handler*
The request handler. -
return
ResponseInterface
Example:
class ApiMiddleware extends Middleware
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$contentType = $request->getHeaderLine('Content-Type');
$key = $request->getHeaderLine('key');
$secret = $request->getHeaderLine('secret');
if ($contentType !== 'application/json') {
return (new Response)->withStatus(406, 'Content type is not accepted.');
}
if ($key !== '23492834234') {
return (new Response)->withStatus(401, 'API key is invalid.');
}
if ($secret !== '1a163782ee166156294d173fcf8b8e87') {
return (new Response)->withStatus(401, 'API secret is invalid.');
}
return $handler->handle($request);
}
}
composer require shieldon/psr-http
Shieldon PSR HTTP implementation written by Terry L. from Taiwan.