@@ -14,7 +14,7 @@ These two tools allow you to model payloads of varying complexity.
14
14
15
15
To allow providing _ representations_ of these, we provide
16
16
` Zend\Expressive\Hal\HalResponseFactory ` . This factory generates a
17
- [ PSR-7] ( http ://www.php-fig.org/psr/psr-7/) response for the provided resource,
17
+ [ PSR-7] ( https ://www.php-fig.org/psr/psr-7/) response for the provided resource,
18
18
including its links and any embedded/child resources it composes.
19
19
20
20
Creating link URIs by hand is error-prone, as URI schemas may change; most
@@ -150,9 +150,9 @@ MetadataMap::class => [
150
150
151
151
### Manually creating and rendering a resource
152
152
153
- The following middleware creates a ` HalResource ` with its associated links, and
154
- then manually renders it using ` Zend\Expressive\Hal\Renderer\JsonRenderer ` . (An
155
- ` XmlRenderer ` is also provided, but not demonstrated here.)
153
+ The following request handler creates a ` HalResource ` with its associated links,
154
+ and then manually renders it using ` Zend\Expressive\Hal\Renderer\JsonRenderer ` .
155
+ (An ` XmlRenderer ` is also provided, but not demonstrated here.)
156
156
157
157
We'll assume that ` Api\Books\Repository ` handles retrieving data from persistent
158
158
storage.
@@ -161,16 +161,16 @@ storage.
161
161
namespace Api\Books\Action;
162
162
163
163
use Api\Books\Repository;
164
- use Interop\Http\ServerMiddleware\DelegateInterface;
165
- use Interop\Http\ServerMiddleware\MiddlewareInterface;
164
+ use Psr\Http\Message\ResponseInterface;
166
165
use Psr\Http\Message\ServerRequestInterface;
166
+ use Psr\Http\Server\RequestHandlerInterface;
167
167
use RuntimeException;
168
168
use Zend\Diactoros\Response\TextResponse;
169
169
use Zend\Expressive\Hal\HalResource;
170
170
use Zend\Expressive\Hal\Link;
171
171
use Zend\Expressive\Hal\Renderer\JsonRenderer;
172
172
173
- class BookAction implements MiddlewareInterface
173
+ class BookAction implements RequestHandlerInterface
174
174
{
175
175
/** @var JsonRenderer */
176
176
private $renderer;
@@ -186,7 +186,7 @@ class BookAction implements MiddlewareInterface
186
186
$this->renderer = $renderer;
187
187
}
188
188
189
- public function process (ServerRequestInterface $request, DelegateInterface $delegate)
189
+ public function handle (ServerRequestInterface $request) : ResponseInterface
190
190
{
191
191
$id = $request->getAttribute('id', false);
192
192
if (! $id) {
@@ -223,28 +223,29 @@ instance. As the complexity of your objects increase, and the number of objects
223
223
you want to represent via HAL increases, you may not want to manually generate
224
224
them.
225
225
226
- ### Middleware using the ResourceGenerator and ResponseFactory
226
+ ### Request handler using the ResourceGenerator and ResponseFactory
227
227
228
- In this next example, our middleware will compose a ` Zend\Expressive\Hal\ResourceGenerator `
229
- instance for generating a ` Zend\Expressive\Hal\HalResource ` from our objects,
230
- and a ` Zend\Expressive\Hal\HalResponseFactory ` for creating a response based on
231
- the returned resource.
228
+ In this next example, our request handler will compose a
229
+ ` Zend\Expressive\Hal\ResourceGenerator ` instance for generating a
230
+ ` Zend\Expressive\Hal\HalResource ` from our objects, and a
231
+ ` Zend\Expressive\Hal\HalResponseFactory ` for creating a response based on the
232
+ returned resource.
232
233
233
- First, we'll look at middleware that displays a single book. We'll assume that
234
+ First, we'll look at a handler that displays a single book. We'll assume that
234
235
` Api\Books\Repository ` handles retrieving data from persistent storage.
235
236
236
237
``` php
237
238
namespace Api\Books\Action;
238
239
239
240
use Api\Books\Repository;
240
- use Interop \Http\ServerMiddleware\DelegateInterface ;
241
- use Interop \Http\ServerMiddleware\MiddlewareInterface ;
242
- use Psr\Http\ServerRequestInterface ;
241
+ use Psr \Http\Message\ResponseInterface ;
242
+ use Psr \Http\Message\ServerRequestInterface ;
243
+ use Psr\Http\Server\RequestHandlerInterface ;
243
244
use RuntimeException;
244
245
use Zend\Expressive\Hal\HalResponseFactory;
245
246
use Zend\Expressive\Hal\ResourceGenerator;
246
247
247
- class BookAction
248
+ class BookAction implements RequestHandlerInterface
248
249
{
249
250
/** @var Repository */
250
251
private $repository;
@@ -265,7 +266,7 @@ class BookAction
265
266
$this->responseFactory = $responseFactory;
266
267
}
267
268
268
- public function __invoke (ServerRequestInterface $request, DelegateInterface $delegate)
269
+ public function handle (ServerRequestInterface $request) : ResponseInterface
269
270
{
270
271
$id = $request->getAttribute('id', false);
271
272
if (! $id) {
@@ -301,9 +302,9 @@ The generated payload might look like the following:
301
302
}
302
303
```
303
304
304
- ### Middleware returning a collection
305
+ ### Request handler returning a collection
305
306
306
- Next, we'll create middleware that returns a _ collection_ of books. The
307
+ Next, we'll create a request handler that returns a _ collection_ of books. The
307
308
collection will be _ paginated_ (assume our repository class creates a
308
309
` BookCollection ` backed by an appropriate adapter), and use a query string
309
310
parameter to determine which page of results to return.
@@ -312,14 +313,14 @@ parameter to determine which page of results to return.
312
313
namespace Api\Books\Action;
313
314
314
315
use Api\Books\Repository;
315
- use Interop \Http\ServerMiddleware\DelegateInterface ;
316
- use Interop \Http\ServerMiddleware\MiddlewareInterface ;
317
- use Psr\Http\ServerRequestInterface ;
316
+ use Psr \Http\Message\ResponseInterface ;
317
+ use Psr \Http\Message\ServerRequestInterface ;
318
+ use Psr\Http\Server\RequestHandlerInterface ;
318
319
use RuntimeException;
319
320
use Zend\Expressive\Hal\HalResponseFactory;
320
321
use Zend\Expressive\Hal\ResourceGenerator;
321
322
322
- class BooksAction
323
+ class BooksAction implements RequestHandlerInterface
323
324
{
324
325
/** @var Repository */
325
326
private $repository;
@@ -340,7 +341,7 @@ class BooksAction
340
341
$this->responseFactory = $responseFactory;
341
342
}
342
343
343
- public function __invoke (ServerRequestInterface $request, DelegateInterface $delegate)
344
+ public function handle (ServerRequestInterface $request) : ResponseInterface
344
345
{
345
346
$page = $request->getQueryParams()['page'] ?? 1;
346
347
0 commit comments