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

Commit 31dd180

Browse files
committed
Document passing JSON encoding flags to JsonResponse
1 parent 704ebda commit 31dd180

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

doc/book/custom-responses.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ response:
6464
$response = new JsonResponse($data, 200, [ 'Content-Type' => ['application/hal+json']]);
6565
```
6666

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+
```
80+
6781
## Empty Responses
6882

6983
Many API actions allow returning empty responses:

src/Response/JsonResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class JsonResponse extends Response
3737
* - JSON_HEX_TAG
3838
* - JSON_HEX_APOS
3939
* - JSON_HEX_AMP
40-
* - JSON_HEX_QUOT options
40+
* - JSON_HEX_QUOT
4141
*
4242
* @param string $data Data to convert to JSON.
4343
* @param int $status Integer status code for the response; 200 by default.

0 commit comments

Comments
 (0)