@@ -20,44 +20,63 @@ Some standard use cases, however, make this un-wieldy:
2020- Returning a redirect response; in this case, you likely just want to specify the target for the
2121 ` Location ` header, and optionally the status code.
2222
23- Starting with version 1.1, Diactoros offers several custom response types and factories for
24- simplifying these common tasks.
23+ Starting with version 1.1, Diactoros offers several custom response types for simplifying these
24+ common tasks.
2525
26- ## String responses
26+ ## HTML Responses
2727
28- ` Zend\Diactoros\Response\StringResponse ` provides factory methods for two standard string response
29- types: HTML and JSON.
30-
31- ### HTML
32-
33- The ` html() ` factory will create a response with the provided HTML as a payload, setting the
28+ ` Zend\Diactoros\Response\HtmlResponse ` allows specifying HTML as a payload, and sets the
3429` Content-Type ` header to ` text/html ` by default:
3530
3631``` php
37- $response = StringResponse::html ($htmlContent);
32+ $response = new HtmlResponse ($htmlContent);
3833```
3934
40- The factory allows passing two additional arguments: a status code, and an array of headers. These
41- allow you to further seed the initial state of the response.
35+ The constructor allows passing two additional arguments: a status code, and an array of headers.
36+ These allow you to further seed the initial state of the response, as well as to override the
37+ ` Content-Type ` header if desired:
38+
39+ ``` php
40+ $response = new HtmlResponse($htmlContent, 200, [ 'Content-Type' => ['application/xhtml+xml']]);
41+ ```
4242
4343Headers must be in the same format as you would provide to the
4444[ Response constructor] [ api.md#response-message ] .
4545
46- ### JSON
47- The ` json() ` factory accepts a data structure to convert to JSON, and returns a response with the
48- JSON content and the ` Content-Type ` header set to ` application/json ` :
46+ ## JSON Responses
47+
48+ ` Zend\Diactoros\Response\JsonResponse ` accepts a data structure to convert to JSON, and sets
49+ the ` Content-Type ` header to ` application/json ` :
4950
5051``` php
51- $response = StringResponse::json ($data);
52+ $response = new JsonResponse ($data);
5253```
5354
5455If a null value is provide, an empty JSON object is used for the content. Scalar data is cast to an
5556array before serialization. If providing an object, we recommend implementing
5657[ JsonSerializable] ( http://php.net/JsonSerializable ) to ensure your object is correctly serialized.
5758
58- Just like the ` html() ` factory , the ` json() ` factory allows passing two additional arguments — a
59+ Just like the ` HtmlResponse ` , the ` JsonResponse ` allows passing two additional arguments — a
5960status code, and an array of headers — to allow you to further seed the initial state of the
60- response.
61+ response:
62+
63+ ``` php
64+ $response = new JsonResponse($data, 200, [ 'Content-Type' => ['application/hal+json']]);
65+ ```
66+
67+ Finally, ` JsonResponse ` allows a fourth optional argument, the flags to provide to ` json_encode() ` .
68+ By default, these are set to ` JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT ` (integer
69+ 15), providing [ RFC 4627] ( http://tools.ietf.org/html/rfc4627 ) compliant JSON capable of embedding in
70+ HTML. If you want to specify a different set of flags, use the fourth constructor argument:
71+
72+ ``` php
73+ $response = new JsonResponse(
74+ $data,
75+ 200,
76+ [],
77+ JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT
78+ );
79+ ```
6180
6281## Empty Responses
6382
@@ -154,13 +173,20 @@ extensions; this is done to protect encapsulation and ensure consistency of oper
154173instances.
155174
156175If you don't want to go the extension route (perhaps you don't want another ` ResponseInterface `
157- implementation within your object graph) you can instead create a factory.
158- [ StringResponse] ( https://github.com/zendframework/zend-diactoros/tree/master/src/Response/StringResponse.php )
159- provides one such example. We recommend the following semantics:
176+ implementation within your object graph) you can instead create a factory. As an example:
160177
161178``` php
162- function ($dataOrMessage, $status = 200, array $headers = []);
179+ $plainTextResponse = function ($text, $status = 200, array $headers = []) {
180+ $response = new Response('php://temp', $status, $headers);
181+ $response->getBody()->write($text);
182+ if (! $response->hasHeader('Content-Type')) {
183+ $response = $response->withHeader('Content-Type', 'text/plain');
184+ }
185+ return $response;
186+ };
187+
188+ $response = $plainTextResponse('Hello, world!');
163189```
164190
165- These ensure consistency of factories, and allow consumers to provide the status and
166- instance-specific headers on creation. (Obviously, specify different defaults as necessary.)
191+ We recommend following the semantic of providing the status and headers as the final two arguments
192+ for any factory or custom response extensions.
0 commit comments