@@ -53,6 +53,13 @@ $route = Literal::factory([
53
53
54
54
Middleware may be provided as PHP callables, or as service names.
55
55
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
+
56
63
> ### No action required
57
64
>
58
65
> Unlike action controllers, middleware typically is single purpose, and, as
@@ -69,8 +76,8 @@ create an error response if non-callable middleware is indicated.
69
76
70
77
## Writing middleware
71
78
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
74
81
middleware signature should look like the following:
75
82
76
83
``` php
@@ -88,14 +95,55 @@ class IndexMiddleware
88
95
}
89
96
```
90
97
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.
93
137
94
138
> ### Routing parameters
95
139
>
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) ` .
99
147
100
148
## Middleware return values
101
149
0 commit comments