Skip to content

Conversation

@cleptric
Copy link
Member

@cleptric cleptric commented Mar 1, 2017

I tried to clarify the use of withBody().
I also fixed some mistakes and restructured the response section a bit.

@cleptric cleptric added this to the 3.x milestone Mar 1, 2017

.. php:method:: withBody($body)
To set the response body, use the `withBody()` method, which is provided by the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backticks for inline code need to be doubled up.


.. php:method:: withStringBody($string)
In most use cases, it is simpler to set a string as the response body::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is simpler, why not put it first?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true :)

// Set a string into the body
$response = $response->withStringBody('My Body');

//If you want a json response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking: Space after double slashes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not nitpicking. Thoroughly 👍

.. versionadded:: 3.4.3
`withStringBody()` was added in 3.4.3

You can also stream responses from files using diactoros streams::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't diactoros a name? Using a capital letter "Diactoros", could make it more clear for non English native speakers...

$stream = new Stream('/path/to/file', 'rb');
$response = $response->withBody($stream);

You can also stream responses from a callback using the ``CallbackStream`` this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"CallbackStream. This"

@markstory markstory self-assigned this Mar 2, 2017

// If you want a json response
$response = $response->withType('application/json')
->withBody(json_encode(['Foo' => 'bar']));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be withStringBody I guess...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thx :)

->withStringBody(json_encode(['Foo' => 'bar']));

.. versionadded:: 3.4.3
`withStringBody()` was added in 3.4.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another place backticks need to be doubled.

// Prior to 3.4.0 - Set the body
$this->response->body('My Body');

Be sure that `$stream` is a :php:class:`Psr\\Http\\Message\\StreamInterface` object.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

@markstory markstory merged commit 6a81f01 into cakephp:3.0 Mar 3, 2017
okinaka added a commit to okinaka/docs that referenced this pull request Mar 4, 2017
okinaka added a commit to okinaka/docs that referenced this pull request Mar 4, 2017
HavokInspiration added a commit that referenced this pull request Jun 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants