You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
return new React\Http\Message\Response(200, [], "hello $role!");
147
+
return new Response(200, [], "Hello $role!\n");
146
148
}
147
149
}
148
150
```
@@ -156,7 +158,7 @@ use Acme\Todo\UserController;
156
158
157
159
// …
158
160
159
-
$app->get('/user/', new AdminMiddleware(), new UserController());
161
+
$app->get('/user', new AdminMiddleware(), new UserController());
160
162
```
161
163
162
164
For example, an HTTP `GET` request for `/user` would first call the middleware handler which then modifies this request and passes the modified request to the next controller function.
@@ -172,7 +174,7 @@ Likewise, we can add an example middleware handler that can modify the outgoing
172
174
173
175
namespace Acme\Todo;
174
176
175
-
use Psr\Http\Message\ResponseInterface);
177
+
use Psr\Http\Message\ResponseInterface;
176
178
use Psr\Http\Message\ServerRequestInterface;
177
179
178
180
class ContentTypeMiddleware
@@ -194,12 +196,13 @@ class ContentTypeMiddleware
194
196
namespace Acme\Todo;
195
197
196
198
use Psr\Http\Message\ServerRequestInterface;
199
+
use React\Http\Message\Response;
197
200
198
201
class UserController
199
202
{
200
203
public function __invoke(ServerRequestInterface $request)
201
204
{
202
-
return new React\Http\Message\Response(200, [], "Hello world!\n");
205
+
return new Response(200, [], "Hello world!\n");
203
206
}
204
207
}
205
208
```
@@ -213,7 +216,7 @@ use Acme\Todo\UserController;
213
216
214
217
// …
215
218
216
-
$app->get('/user/', new ContentTypeMiddleware(), new UserController());
219
+
$app->get('/user', new ContentTypeMiddleware(), new UserController());
217
220
```
218
221
219
222
For example, an HTTP `GET` request for `/user` would first call the middleware handler which passes on the request to the controller function and then modifies the response that is returned by the controller function.
@@ -242,15 +245,10 @@ As a consequence, each middleware handler can also return
242
245
243
246
## Global middleware
244
247
245
-
> ℹ️ **Feature preview**
246
-
>
247
-
> This is a feature preview, i.e. it might not have made it into the current beta.
248
-
> Give feedback to help us prioritize.
249
-
250
-
Additionally, you can also add middleware to the `App` object itself to register
251
-
a global middleware handler for all registered routes:
248
+
Additionally, you can also add middleware to the [`App`](app.md) object itself
249
+
to register a global middleware handler:
252
250
253
-
```php hl_lines="8"
251
+
```php hl_lines="7"
254
252
# app.php
255
253
<?php
256
254
@@ -264,6 +262,9 @@ $app->get('/user', new UserController());
264
262
$app->run();
265
263
```
266
264
265
+
Any global middleware handler will always be called for all registered routes
266
+
and also any requests that can not be routed.
267
+
267
268
You can also combine global middleware handlers (think logging) with additional
268
269
middleware handlers for individual routes (think authentication).
269
270
Global middleware handlers will always be called before route middleware handlers.
0 commit comments