Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 52adc71

Browse files
committed
Adds namespaces in doc section for custom responses
1 parent 17b6fe3 commit 52adc71

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

docs/book/v2/custom-responses.md

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ When developing server-side applications, the message type you're most likely to
44
the 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+
711
class 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

3539
The 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

5058
The constructor allows passing two additional arguments: a status code, and an array of headers.
5159
These 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

5870
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
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

7082
The constructor allows passing two additional arguments: a status code, and an array of headers.
7183
These 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

7894
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
84100
the `Content-Type` header to `application/json`:
85101

86102
```php
87-
$response = new JsonResponse($data);
103+
$response = new Zend\Diactoros\Response\JsonResponse($data);
88104
```
89105

90106
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
95111
response:
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

101121
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
104124
HTML. 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
128148
only:
129149

130150
```php
151+
namespace Zend\Diactoros\Response;
152+
153+
use Zend\Diactoros\Response;
154+
131155
class 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
138162
the 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

@@ -158,6 +184,10 @@ redirect responses. The only required argument is a URI, which may be provided a
158184
are 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+
161191
class RedirectResponse extends Response
162192
{
163193
public function __construct($uri, $status = 302, array $headers = []);
@@ -167,6 +197,8 @@ class RedirectResponse extends Response
167197
Typical 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.
189221
The general pattern will be something like this:
190222

191223
```php
224+
use Zend\Diactoros\Response;
225+
192226
class 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

Comments
 (0)