1. Go to this page and download the library: Download orbiter/middleware-utils library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
orbiter / middleware-utils example snippets
use Orbiter\MiddlewareUtils\HasResponseFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class SampleMiddleware implements MiddlewareInterface {
use HasResponseFactory;
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
if($request->hasHeader('error') === 'some-strange-bug') {
// Simple Status Response
return $this->create500();
// Same like `create500`
return $this->createResponse(500, 'Internal Server Error');
}
if($request->hasHeader('error') === 'client-error') {
// Creating: 400 Bad Request with JSON Body
return $this->respondJson($this->create400(), ['message' => 'client-error-msg']);
}
return $handler->handle($request);
}
}
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Orbiter\MiddlewareUtils\HasResponseFactory;
use Orbiter\MiddlewareUtils\ApiError;
class SampleMiddleware implements MiddlewareInterface {
use HasResponseFactory;
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
if($request->hasHeader('error') === 'user-not-found') {
// Creating: 400 Bad Request with ApiError as JSON Body
return $this->respondJson($this->create404(), new ApiError('User Not Found'));
}
return $handler->handle($request);
}
}