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

Commit 0ada590

Browse files
committed
Demonstrate using keys for middleware specs
By using keys, you can have multiple configurations that are related, and which will merge into a single pipeline. This also allows us to have a common "routing" key for the routing middleware, which makes it easier to identify and replace if desired.
1 parent a79eccf commit 0ada590

11 files changed

+69
-17
lines changed

doc/book/container/factories.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ instance.
3535
When the `config` service is present, the factory can utilize several keys in
3636
order to seed the `Application` instance:
3737

38-
- `middleware_pipeline` can be used to seed pre- and/or post-routing middleware:
38+
- `middleware_pipeline` can be used to seed the middleware pipeline:
3939

4040
```php
4141
'middleware_pipeline' => [
@@ -75,6 +75,10 @@ order to seed the `Application` instance:
7575
granular priority values to specify the order in which middleware should be
7676
piped to the application.
7777

78+
You *can* specify keys for each middleware specification. These will be
79+
ignored by the factory, but can be useful when merging several configurations
80+
into one for the application.
81+
7882
- `routes` is used to define routed middleware. The value must be an array,
7983
consisting of arrays defining each middleware:
8084

doc/book/cookbook/custom-404-page-handling.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ configuration, after the dispatch middleware:
9292
```php
9393
'middleware_pipeline' => [
9494
/* ... */
95-
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
95+
'routing' => [
96+
'middleware' => [
97+
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
98+
Zend\Expressive\Helper\UrlHelperMiddleware::class,
99+
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
100+
],
101+
'priority' => 1,
102+
],
96103
[
97104
'middleware' => 'Application\NotFound',
98105
'priority' => -1,

doc/book/cookbook/route-specific-pipeline.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ answer is that the syntax is exactly the same!
168168
```php
169169
return [
170170
'middleware_pipeline' => [
171-
[
171+
'api' => [
172172
'path' => '/api',
173173
'middleware' => [
174174
'AuthenticationMiddleware',

doc/book/cookbook/setting-locale-depending-routing-parameter.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ return [
142142
[
143143
'middleware' => [
144144
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
145+
Helper\UrlHelperMiddleware::class,
145146
[ 'middleware' => LocalizationMiddleware::class ],
146147
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
147148
],

doc/book/cookbook/setting-locale-without-routing-parameter.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,11 @@ return [
112112

113113
/* ... */
114114

115-
[
115+
'routing' => [
116116
'middleware' => [
117117
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
118118
Zend\Expressive\Helper\UrlHelperMiddleware::class,
119119
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
120-
/* ... */
121120
],
122121
'priority' => 1,
123122
],

doc/book/cookbook/using-a-base-path.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,14 @@ return [
108108
'middleware_pipeline' => [
109109
[ 'middleware' => [ Blast\BaseUrl\BaseUrlMiddleware::class ], 'priority' => 1000 ],
110110
/* ... */
111-
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
111+
'routing' => [
112+
'middleware' => [
113+
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
114+
Zend\Expressive\Helper\UrlHelperMiddleware::class,
115+
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
116+
],
117+
'priority' => 1,
118+
],
112119
/* ... */
113120
],
114121
];

doc/book/cookbook/using-zend-form-view-helpers.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,14 @@ return [
226226
'middleware_pipeline' => [
227227
['middleware' => Your\Application\FormHelpersMiddleware::class, 'priority' => 1000],
228228
/* ... */
229-
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
229+
'routing' => [
230+
'middleware' => [
231+
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
232+
Zend\Expressive\Helper\UrlHelperMiddleware::class,
233+
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
234+
],
235+
'priority' => 1,
236+
],
230237
/* ... */
231238
],
232239
];

doc/book/helpers/body-parse.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ return [
4545
'middleware_pipeline' => [
4646
[ 'middleware' => Helper\BodyParams\BodyParamsMiddleware::class, 'priority' => 100],
4747
/* ... */
48-
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
48+
'routing' => [
49+
'middleware' => [
50+
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
51+
Helper\UrlHelperMiddleware::class,
52+
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
53+
],
54+
'priority' => 1,
55+
],
4956
/* ... */
5057
],
5158
];

doc/book/helpers/url-helper.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ $app->pipeDispatchMiddleware();
140140
// [
141141
// 'middleware_pipeline' => [
142142
// /* ... */
143-
// [
143+
// 'routing' => [
144144
// 'middleware' => [
145145
// Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
146146
// UrlHelperMiddleware::class
@@ -181,7 +181,7 @@ return [
181181
],
182182
],
183183
'middleware_pipeline' => [
184-
[
184+
'routing' => [
185185
'middleware' => [
186186
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
187187
UrlHelperMiddleware::class,

doc/book/migration/rc-to-v1.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,20 +298,20 @@ This would be rewritten to the following to work with RC6 and later:
298298
```php
299299
return [
300300
'middleware_pipeline' => [
301-
[
301+
'always' => [
302302
'middleware' => [
303303
Zend\Expressive\Helper\ServerUrlMiddleware::class,
304304
DebugToolbarMiddleware::class,
305305
],
306306
'priority' => PHP_INT_MAX,
307307
],
308-
[
308+
'api' => [
309309
'middleware' => ApiMiddleware::class,
310310
'path' => '/api,
311311
'priority' => 100,
312312
],
313313

314-
[
314+
'routing' => [
315315
'middleware' => [
316316
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
317317
Zend\Expressive\Helper\UrlHelperMiddleware::class,
@@ -320,7 +320,7 @@ return [
320320
'priority' => 1,
321321
],
322322

323-
[
323+
'error' => [
324324
'middleware' => [
325325
NotFoundMiddleware::class,
326326
],
@@ -336,6 +336,16 @@ priority, you can simplify adding new middleware, particularly if you know it
336336
should execute before routing, or as error middleware, or between routing and
337337
dispatch.
338338

339+
> #### Keys are ignored
340+
>
341+
> The above example provides keys for each middleware specification. The factory
342+
> will ignore these, but they can be useful for cases when you might want to
343+
> specify configuration in multiple files, and merge specific entries together.
344+
> Be aware, however, that the `middleware` key itself is an indexed array;
345+
> items will be appended based on the order in which configuration files are
346+
> merged. If order of these is important, create separate specifications with
347+
> relevant `priority` values.
348+
339349
## Route result observer deprecation
340350

341351
As of RC6, the following changes have occurred with regards to route result
@@ -378,7 +388,7 @@ however, you will need to register it following the routing middleware:
378388
[
379389
'middleware_pipeline' => [
380390
/* ... */
381-
[
391+
'routing' => [
382392
'middleware' => [
383393
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
384394
Zend\Expressive\Container\ApplicationFactory::ROUTE_RESULT_OBSERVER_MIDDLEWARE,
@@ -460,7 +470,7 @@ If you are using the `ApplicationFactory`, alter your configuration:
460470
[
461471
'middleware_pipeline' => [
462472
/* ... */
463-
[
473+
'routing' => [
464474
'middleware' => [
465475
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
466476
['middleware' => MyObserver::class],

0 commit comments

Comments
 (0)