Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 665dfbe

Browse files
committed
Updated middleware listener documentation to reflect #217
1 parent 9c4632e commit 665dfbe

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

doc/book/middleware.md

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ $route = Literal::factory([
5353

5454
Middleware may be provided as PHP callables, or as service names.
5555

56+
**As of 3.1.0** you may also specify an `array` of middleware, and middleware
57+
may be [http-interop/http-middleware](https://github.com/http-interop/http-middleware)
58+
compatible. Each item in the array must be a PHP callable, service name, or
59+
http-middleware instance. These will then be piped into a
60+
`Zend\Stratigility\MiddlewarePipe` instance in the order in which they are
61+
present in the array.
62+
5663
> ### No action required
5764
>
5865
> Unlike action controllers, middleware typically is single purpose, and, as
@@ -69,8 +76,8 @@ create an error response if non-callable middleware is indicated.
6976

7077
## Writing middleware
7178

72-
When dispatching middleware, the `MiddlewareListener` calls it with two
73-
arguments, the PSR-7 request and response, respectively. As such, your
79+
Prior to 3.1.0, when dispatching middleware, the `MiddlewareListener` calls it
80+
with two arguments, the PSR-7 request and response, respectively. As such, your
7481
middleware signature should look like the following:
7582

7683
```php
@@ -88,14 +95,55 @@ class IndexMiddleware
8895
}
8996
```
9097

91-
From there, you can pull information from the composed request, and manipulate
92-
the response.
98+
Starting in 3.1.0, the `MiddlewareListener` always adds middleware to a
99+
`Zend\Stratigility\MiddlewarePipe` instance, and invokes it as
100+
[http-interop/http-middleware](https://github.com/http-interop/http-middleware),
101+
passing it a PSR-7 `ServerRequestInterface` and an http-interop
102+
`DelegateInterface`.
103+
104+
As such, ideally your middleware should implement the `MiddlewareInterface` from
105+
[http-interop/http-middleware](https://github.com/http-interop/http-middleware):
106+
107+
```php
108+
namespace Application\Middleware;
109+
110+
use Interop\Http\ServerMiddleware\DelegateInterface;
111+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
112+
use Psr\Http\Message\ServerRequestInterface;
113+
114+
class IndexMiddleware implements MiddlewareInterface
115+
{
116+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
117+
{
118+
// do some work
119+
}
120+
}
121+
```
122+
123+
Alternately, you may still write `callable` middleware using the following
124+
signature:
125+
126+
```php
127+
function (ServerREquestInterface $request, ResponseInterface $response, callable $next)
128+
{
129+
// do some work
130+
}
131+
```
132+
133+
In the above case, the `DelegateInterface` is decorated as a callable.
134+
135+
In all versions, within your middleware, you can pull information from the
136+
composed request, and return a response.
93137

94138
> ### Routing parameters
95139
>
96-
> At the time of the 2.7.0 release, route match parameters are not yet injected
97-
> into the PSR-7 `ServerRequest` instance, and are thus not available as request
98-
> attributes..
140+
> At the time of the 2.7.0 release, route match parameters were not yet injected
141+
> into the PSR-7 `ServerRequest` instance, and thus not available as request
142+
> attributes.
143+
>
144+
> With the 3.0 release, they are pushed into the PSR-7 `ServerRequest` as
145+
> attributes, and may thus be fetched using
146+
> `$request->getAttribute($attributeName)`.
99147
100148
## Middleware return values
101149

0 commit comments

Comments
 (0)