@@ -4,6 +4,10 @@ When developing server-side applications, the message type you're most likely to
4
4
the response. In such cases, the standard signature can be an obstacle to usability. Let's review:
5
5
6
6
``` php
7
+ namespace Zend\Diactoros;
8
+
9
+ use Psr\Http\Message\ResponseInterface;
10
+
7
11
class Response implements ResponseInterface
8
12
{
9
13
public function __construct($body = 'php://temp', $status = 200, array $headers = []);
@@ -29,13 +33,17 @@ common tasks.
29
33
` Content-Type ` header to ` text/plain ` by default:
30
34
31
35
``` php
32
- $response = new TextResponse('Hello world!');
36
+ $response = new Zend\Diactoros\Response\ TextResponse('Hello world!');
33
37
```
34
38
35
39
The constructor accepts two additional arguments: a status code and an array of headers.
36
40
37
41
``` php
38
- $response = new TextResponse($text, 200, ['Content-Type' => ['text/csv']]);
42
+ $response = new Zend\Diactoros\Response\TextResponse(
43
+ $text,
44
+ 200,
45
+ ['Content-Type' => ['text/csv']]
46
+ );
39
47
```
40
48
41
49
## HTML Responses
@@ -44,15 +52,19 @@ $response = new TextResponse($text, 200, ['Content-Type' => ['text/csv']]);
44
52
` Content-Type ` header to ` text/html ` by default:
45
53
46
54
``` php
47
- $response = new HtmlResponse($htmlContent);
55
+ $response = new Zend\Diactoros\Response\ HtmlResponse($htmlContent);
48
56
```
49
57
50
58
The constructor allows passing two additional arguments: a status code, and an array of headers.
51
59
These allow you to further seed the initial state of the response, as well as to override the
52
60
` Content-Type ` header if desired:
53
61
54
62
``` php
55
- $response = new HtmlResponse($htmlContent, 200, [ 'Content-Type' => ['application/xhtml+xml']]);
63
+ $response = new Zend\Diactoros\Response\HtmlResponse(
64
+ $htmlContent,
65
+ 200,
66
+ ['Content-Type' => ['application/xhtml+xml']]
67
+ );
56
68
```
57
69
58
70
Headers must be in the same format as you would provide to the
@@ -64,15 +76,19 @@ Headers must be in the same format as you would provide to the
64
76
` Content-Type ` header to ` application/xml ` by default:
65
77
66
78
``` php
67
- $response = new XmlResponse($xml);
79
+ $response = new Zend\Diactoros\Response\ XmlResponse($xml);
68
80
```
69
81
70
82
The constructor allows passing two additional arguments: a status code, and an array of headers.
71
83
These allow you to further seed the initial state of the response, as well as to override the
72
84
` Content-Type ` header if desired:
73
85
74
86
``` php
75
- $response = new XmlResponse($xml, 200, [ 'Content-Type' => ['application/hal+xml']]);
87
+ $response = new Zend\Diactoros\Response\XmlResponse(
88
+ $xml,
89
+ 200,
90
+ ['Content-Type' => ['application/hal+xml']]
91
+ );
76
92
```
77
93
78
94
Headers must be in the same format as you would provide to the
@@ -84,7 +100,7 @@ Headers must be in the same format as you would provide to the
84
100
the ` Content-Type ` header to ` application/json ` :
85
101
86
102
``` php
87
- $response = new JsonResponse($data);
103
+ $response = new Zend\Diactoros\Response\ JsonResponse($data);
88
104
```
89
105
90
106
If providing an object, we recommend implementing [ JsonSerializable] ( http://php.net/JsonSerializable )
@@ -95,7 +111,11 @@ status code, and an array of headers — to allow you to further seed the initia
95
111
response:
96
112
97
113
``` php
98
- $response = new JsonResponse($data, 200, [ 'Content-Type' => ['application/hal+json']]);
114
+ $response = new Zend\Diactoros\Response\JsonResponse(
115
+ $data,
116
+ 200,
117
+ ['Content-Type' => ['application/hal+json']]
118
+ );
99
119
```
100
120
101
121
Finally, ` JsonResponse ` allows a fourth optional argument, the flags to provide to ` json_encode() ` .
@@ -104,7 +124,7 @@ By default, these are set to `JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON
104
124
HTML. If you want to specify a different set of flags, use the fourth constructor argument:
105
125
106
126
``` php
107
- $response = new JsonResponse(
127
+ $response = new Zend\Diactoros\Response\ JsonResponse(
108
128
$data,
109
129
200,
110
130
[],
@@ -128,6 +148,10 @@ returns an empty response with a 204 status. Its constructor allows passing the
128
148
only:
129
149
130
150
``` php
151
+ namespace Zend\Diactoros\Response;
152
+
153
+ use Zend\Diactoros\Response;
154
+
131
155
class EmptyResponse extends Response
132
156
{
133
157
public function __construct($status = 204, array $headers = []);
@@ -138,6 +162,8 @@ An empty, read-only body is injected at instantiation, ensuring no write operati
138
162
the response. Usage is typically one of the following forms:
139
163
140
164
``` php
165
+ use Zend\Diactoros\Response\EmptyResponse;
166
+
141
167
// Basic 204 response:
142
168
$response = new EmptyResponse();
143
169
@@ -158,6 +184,10 @@ redirect responses. The only required argument is a URI, which may be provided a
158
184
are produced; you may alter these via the additional optional arguments:
159
185
160
186
``` php
187
+ namespace Zend\Diactoros\Response;
188
+
189
+ use Zend\Diactoros\Response;
190
+
161
191
class RedirectResponse extends Response
162
192
{
163
193
public function __construct($uri, $status = 302, array $headers = []);
@@ -167,6 +197,8 @@ class RedirectResponse extends Response
167
197
Typical usage is:
168
198
169
199
``` php
200
+ use Zend\Diactoros\Response\RedirectResponse;
201
+
170
202
// 302 redirect:
171
203
$response = new RedirectResponse('/user/login');
172
204
@@ -189,6 +221,8 @@ create your custom types.
189
221
The general pattern will be something like this:
190
222
191
223
``` php
224
+ use Zend\Diactoros\Response;
225
+
192
226
class MyCustomResponse extends Response
193
227
{
194
228
public function __construct($data, $status = 200, array $headers = [])
@@ -211,7 +245,7 @@ implementation within your object graph) you can instead create a factory. As an
211
245
212
246
``` php
213
247
$plainTextResponse = function ($text, $status = 200, array $headers = []) {
214
- $response = new Response('php://temp', $status, $headers);
248
+ $response = new Zend\Diactoros\ Response('php://temp', $status, $headers);
215
249
$response->getBody()->write($text);
216
250
if (! $response->hasHeader('Content-Type')) {
217
251
$response = $response->withHeader('Content-Type', 'text/plain');
0 commit comments