|
| 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 | +``` |
0 commit comments