dev-master
9999999-dev
MIT
The Requires
- php >=5.4
- psr/http-message ^1.0
The Development Requires
middleware psr-7 http
0.1.0
0.1.0.0
MIT
The Requires
- php >=5.4
- psr/http-message ^1.0
The Development Requires
middleware psr-7 http
Wallogit.com
2017 © Pedro Peláez
No longer maintained, (*1)
Create callable middlewares instead. Signature:, (*2)
function (RequestInterface $request, ResponseInterface $response, callable $next) {
// Do something with the request before passing it on.
...
// Pass on to next middleware.
$response = $next($request, $response);
// Do something with the response after the next middleware has been called.
...
// Return ultimate response object.
return $response;
}
Stackable middlewares for PSR-7 HTTP message objects., (*4)
composer require hannesvdvreken/psr7-middlewares
This package is a small library that helps you to construct a decorated array of middlewares., (*5)
There are 2 types of middlewares:, (*6)
A Kernel, which is a middleware which can pass on the Request and Response object to the next layer.
A Core object is usually the last layer of a list of middlewares.
A core will always return a PSR-7 ResponseInterface object and never pass on
the given RequestInterface object to a next layer. It will never have a next middleware set., (*7)
The Builder object helps in creating a composed Core which consists of a specified list of layers.
Note that the builder object is immutable:
thus it returns a different mutated object after each push and unshift call., (*8)
use Psr7Stack\Builder; $builder = new Builder(); $session = new SessionMiddleware(); $throttle = new TrottleMiddleware(); $app = new App(); $builder = $buider->push($session)->push($throttle)->push($app); $stack = $builder->resolve();
The returned Stack object can also be created with the static factory method create., (*9)
use Psr7Stack\Stack; $stack = Stack::create([$session, $throttle, $app]);
The Stack object itself is also a Core middleware, so it can be used in a different composition of middlewares. This is how you can send a Request object through the different layers of middlewares:, (*10)
$psrResponse = $stack->handle($psrRequest);
Creating a Core yourself:, (*11)
use Psr7Stack\Core;
use Psr\Http\Message\RequestInterface;
class App implements Core
{
/**
* @param \Psr\Http\Message\RequestInterface $request
*
* @request \Psr\Http\Message\ResponseInterface
*/
public function handle(RequestInterface $request)
{
// Call the router and return the response.
return $response;
}
}
Creating a Kernel:, (*12)
use Psr7Stack\Kernel;
use Psr7Stack\Traits\NextCore;
class SessionMiddleware implements Kernel
{
// Use trait to implement the setNextCore method.
use NextCore;
/**
* @param \Psr\Http\Message\RequestInterface $request
*
* @request \Psr\Http\Message\ResponseInterface
*/
public function handle(RequestInterface $request)
{
// Call the next core and return the response.
$response = $this->next->handle($request);
// Do something with the response and return it.
...
return $response;
}
}
Contributions are welcome. See the contributions file to know how to contribute., (*13)
The MIT License (MIT). Please see License File for more information., (*14)
MIT
middleware psr-7 http
MIT
middleware psr-7 http