POST /path HTTP/1.1
Host: example.com
AnotherHeader: AnotherHeaderValue
foo=bar&baz=bat
HTTP/1.1 200 OK
Content-Type: text/plain
AnotherHeader: AnotherHeaderValue
This is the response body
$baseUri = new Uri('http://api.example.com');
$baseRequest = new Request($baseUri, null, [
'Authorization' => 'Bearer ' . $token,
'Accept' => 'application/json',
]);;
$uri = $baseUri->withPath('/user');
$request = $baseRequest->withUri($uri)
->withMethod('GET');
Implementations list available on http://bit.ly/1OHQJHd
function (
Psr\Http\Message\ServerRequestInterface $request,
Psr\Http\Message\ResponseInterface $response,
callable $next = null
) {
}
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\ServerMiddleware\MiddlewareInterface;
class MyMiddleware implements MiddlewareInterface
{
public function process(
ServerRequestInterface $request,
DelegateInterface $delegate
) {
return new Response();
// or call the next middleware in the queue
return $delegate->process($request);
}
}
proposed PSR-15 standard as of now
$app = new MiddlewareRunner();
$app->add(new Versioning());
$app->add(new Router());
$app->add(new Authentication());
$app->add(new Options());
$app->add(new Authorization());
$app->add(new ContentNegotiation());
$app->add(new ContentType());
$app->add(new Dispatcher());
$app->add(new ProblemHandler());
$app->run($request, $response);
You get to control the workflow of your application by deciding the order in which middleware is queued
$app = new MiddlewareRunner();
$app->add('/contact', new ContactFormMiddleware());
$app->add('/forum', new ForumMiddleware());
$app->add('/blog', new BlogMiddleware());
$app->add('/store', new EcommerceMiddleware());
$app->run($request, $response);
$app = new Zend\Stratigility\MiddlewarePipe();
// Landing page
$app->pipe('/', function ($req, $res, $next) {
if (! in_array($req->getUri()->getPath(), ['/', ''], true)) {
return $next($req, $res);
}
return $res->end('Hello world!');
});
// Another page
$app->pipe('/foo', function ($req, $res, $next) {
return $res->end('FOO!');
});