Skip to content

Commit 5c2cc56

Browse files
authored
Document withStringBody() vs auto-render interaction (#8315)
* Document that withStringBody() requires returning the response or disabling auto-render * Fix MD031 blank line around fenced code block
1 parent 36f4721 commit 5c2cc56

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

docs/en/controllers/request-response.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,31 @@ $response = $response->withType('application/json')
831831
->withStringBody(json_encode(['Foo' => 'bar']));
832832
```
833833

834+
::: warning
835+
Setting a string body alone is not enough to send it. If your action neither
836+
returns the response nor disables view rendering, the controller still calls
837+
`render()` and overwrites the body you set. To make the string body take effect,
838+
either return the response from the action:
839+
840+
```php
841+
public function export()
842+
{
843+
return $this->response->withStringBody('My Body');
844+
}
845+
```
846+
847+
or set it and disable auto-render:
848+
849+
```php
850+
public function export()
851+
{
852+
$this->setResponse($this->response->withStringBody('My Body'));
853+
$this->disableAutoRender();
854+
}
855+
```
856+
857+
:::
858+
834859
`method` Cake\\Http\\Response::**withBody**(StreamInterface $body): static
835860

836861
To set the response body, use the `withBody()` method, which is provided by the

0 commit comments

Comments
 (0)