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

Commit c83fe64

Browse files
committed
Merge branch 'feature/457' into develop
Close #457
2 parents 5e34b9b + 93dac06 commit c83fe64

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Passing Data Between Middleware
2+
3+
A frequently asked question is how to pass data between middleware.
4+
5+
The answer is present in every middleware: via request object attributes.
6+
7+
Middleware is always executed in the order in which it is piped to the
8+
application. This way you can ensure the request object in middleware receiving
9+
data contains an attribute containing data passed by outer middleware.
10+
11+
In the following example, `PassingDataMiddleware` prepares data to pass as a
12+
request attribute to nested middleware. We use the fully qualified class name
13+
for the attribute name to ensure uniqueness, but you can name it anything you
14+
want.
15+
16+
```php
17+
namespace App\Middleware;
18+
19+
use Interop\Http\ServerMiddleware\DelegateInterface;
20+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
21+
use Psr\Http\Message\ServerRequestInterface;
22+
23+
class PassingDataMiddleware implements MiddlewareInterface
24+
{
25+
// ...
26+
27+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
28+
{
29+
// Step 1: Do something first
30+
$data = [
31+
'foo' => 'bar',
32+
];
33+
34+
// Step 2: Inject data into the request, call the next middleware and wait for the response
35+
$response = $delegate->process($request->withAttribute(self::class, $data));
36+
37+
// Step 3: Optionally, do something (with the response) before returning the response
38+
39+
// Step 4: Return the response
40+
return $response;
41+
}
42+
}
43+
```
44+
45+
Later, `ReceivingDataMiddleware` grabs the data and processes it:
46+
47+
```php
48+
namespace App\Middleware;
49+
50+
use Interop\Http\ServerMiddleware\DelegateInterface;
51+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
52+
use Psr\Http\Message\ServerRequestInterface;
53+
54+
class ReceivingDataMiddleware implements MiddlewareInterface
55+
{
56+
// ...
57+
58+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
59+
{
60+
// Step 1: Grab the data from the request and use it
61+
$data = $request->getAttribute(PassingDataMiddleware::class);
62+
63+
// Step 2: Call the next middleware and wait for the response
64+
$response = $delegate->process($request);
65+
66+
// Step 3: Optionally, do something (with the response) before returning the response
67+
68+
// Step 4: Return the response
69+
return $response;
70+
}
71+
}
72+
```
73+
74+
Of course, you could also use the data in routed middleware, which is usually at
75+
the innermost layer of your application. The `ExampleAction` below takes that
76+
information and passes it to the template renderer to create an `HtmlResponse`:
77+
78+
```php
79+
namespace App\Action;
80+
81+
use Interop\Http\ServerMiddleware\DelegateInterface;
82+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
83+
use Psr\Http\Message\ServerRequestInterface;
84+
use Zend\Diactoros\Response\HtmlResponse;
85+
86+
class ExampleAction implements MiddlewareInterface
87+
{
88+
// ...
89+
90+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
91+
{
92+
// Step 1: Grab the data from the request
93+
$data = $request->getAttribute(PassingDataMiddleware::class);
94+
$id = $request->getAttribute('id');
95+
96+
// Step 2: Do some more stuff
97+
98+
// Step 3: Return a Response
99+
return new HtmlResponse(
100+
$this->templateRenderer->render('blog::entry', [
101+
'data' => $data,
102+
'id' => $id,
103+
])
104+
);
105+
}
106+
}
107+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pages:
5454
- 'Enabling debug toolbars': cookbook/debug-toolbars.md
5555
- 'Handling multiple routes in a single class': cookbook/using-routed-middleware-class-as-controller.md
5656
- 'Flash Messengers': cookbook/flash-messengers.md
57+
- 'Passing data between middleware': cookbook/passing-data-between-middleware.md
5758
- Reference:
5859
- "Why choose Expressive?": why-expressive.md
5960
- Examples: reference/usage-examples.md

0 commit comments

Comments
 (0)