@@ -4,6 +4,10 @@ When developing server-side applications, the message type you're most likely to
44the response. In such cases, the standard signature can be an obstacle to usability. Let's review:
55
66``` php
7+ namespace Zend\Diactoros;
8+
9+ use Psr\Http\Message\ResponseInterface;
10+
711class Response implements ResponseInterface
812{
913 public function __construct($body = 'php://temp', $status = 200, array $headers = []);
@@ -29,13 +33,17 @@ common tasks.
2933` Content-Type ` header to ` text/plain ` by default:
3034
3135``` php
32- $response = new TextResponse('Hello world!');
36+ $response = new Zend\Diactoros\Response\ TextResponse('Hello world!');
3337```
3438
3539The constructor accepts two additional arguments: a status code and an array of headers.
3640
3741``` 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+ );
3947```
4048
4149## HTML Responses
@@ -44,15 +52,19 @@ $response = new TextResponse($text, 200, ['Content-Type' => ['text/csv']]);
4452` Content-Type ` header to ` text/html ` by default:
4553
4654``` php
47- $response = new HtmlResponse($htmlContent);
55+ $response = new Zend\Diactoros\Response\ HtmlResponse($htmlContent);
4856```
4957
5058The constructor allows passing two additional arguments: a status code, and an array of headers.
5159These allow you to further seed the initial state of the response, as well as to override the
5260` Content-Type ` header if desired:
5361
5462``` 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+ );
5668```
5769
5870Headers 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
6476` Content-Type ` header to ` application/xml ` by default:
6577
6678``` php
67- $response = new XmlResponse($xml);
79+ $response = new Zend\Diactoros\Response\ XmlResponse($xml);
6880```
6981
7082The constructor allows passing two additional arguments: a status code, and an array of headers.
7183These allow you to further seed the initial state of the response, as well as to override the
7284` Content-Type ` header if desired:
7385
7486``` 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+ );
7692```
7793
7894Headers 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
84100the ` Content-Type ` header to ` application/json ` :
85101
86102``` php
87- $response = new JsonResponse($data);
103+ $response = new Zend\Diactoros\Response\ JsonResponse($data);
88104```
89105
90106If 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
95111response:
96112
97113``` 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+ );
99119```
100120
101121Finally, ` 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
104124HTML. If you want to specify a different set of flags, use the fourth constructor argument:
105125
106126``` php
107- $response = new JsonResponse(
127+ $response = new Zend\Diactoros\Response\ JsonResponse(
108128 $data,
109129 200,
110130 [],
@@ -128,6 +148,10 @@ returns an empty response with a 204 status. Its constructor allows passing the
128148only:
129149
130150``` php
151+ namespace Zend\Diactoros\Response;
152+
153+ use Zend\Diactoros\Response;
154+
131155class EmptyResponse extends Response
132156{
133157 public function __construct($status = 204, array $headers = []);
@@ -138,6 +162,8 @@ An empty, read-only body is injected at instantiation, ensuring no write operati
138162the response. Usage is typically one of the following forms:
139163
140164``` php
165+ use Zend\Diactoros\Response\EmptyResponse;
166+
141167// Basic 204 response:
142168$response = new EmptyResponse();
143169
@@ -147,7 +173,7 @@ $response = new EmptyResponse(201, [
147173]);
148174
149175// Alternately, set the header after instantiation:
150- $response = ( new EmptyResponse(201) )->withHeader('Location', $url);
176+ $response = (new EmptyResponse(201))->withHeader('Location', $url);
151177```
152178
153179## Redirects
@@ -158,6 +184,10 @@ redirect responses. The only required argument is a URI, which may be provided a
158184are produced; you may alter these via the additional optional arguments:
159185
160186``` php
187+ namespace Zend\Diactoros\Response;
188+
189+ use Zend\Diactoros\Response;
190+
161191class RedirectResponse extends Response
162192{
163193 public function __construct($uri, $status = 302, array $headers = []);
@@ -167,6 +197,8 @@ class RedirectResponse extends Response
167197Typical usage is:
168198
169199``` php
200+ use Zend\Diactoros\Response\RedirectResponse;
201+
170202// 302 redirect:
171203$response = new RedirectResponse('/user/login');
172204
@@ -189,6 +221,8 @@ create your custom types.
189221The general pattern will be something like this:
190222
191223``` php
224+ use Zend\Diactoros\Response;
225+
192226class MyCustomResponse extends Response
193227{
194228 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
211245
212246``` php
213247$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);
215249 $response->getBody()->write($text);
216250 if (! $response->hasHeader('Content-Type')) {
217251 $response = $response->withHeader('Content-Type', 'text/plain');
0 commit comments