Ensure withJson returns a new Response #1975
Closed
Conversation
Fixes issue 1818. The withJson method now clones the Response, and updates this with a new header, and a new Body.
Slim/Http/Response.php
Outdated
| $body->rewind(); | ||
| $body->write($json = json_encode($data, $encodingOptions)); | ||
| $clone = clone $this; | ||
| $clone->body = new Body(fopen('php://temp', 'w+')); |
mathmarques
Sep 2, 2016
Contributor
I think it's better to use $newResponse = $this->withBody(new Body(fopen('php://temp', 'r+'))) instead of clone and set.
I think it's better to use $newResponse = $this->withBody(new Body(fopen('php://temp', 'r+'))) instead of clone and set.
akrabat
Sep 2, 2016
Member
Where does newResponse come from?
I like clone as it's quick.
Where does newResponse come from?
I like clone as it's quick.
mathmarques
Sep 2, 2016
•
Contributor
I was just saying to use withBody, cause if we have a method for return a new response with a new body, we don't need to reimplement it.
I was just saying to use withBody, cause if we have a method for return a new response with a new body, we don't need to reimplement it.
robertprice
Sep 2, 2016
•
Author
Contributor
I think @mathmarques makes a good point. withBody clones and sets the body, so it's the same code really. It may make more sense to do this instead of duplicating code.
I think @mathmarques makes a good point. withBody clones and sets the body, so it's the same code really. It may make more sense to do this instead of duplicating code.
Changed withJson to use withBody to create a new Response instead of cloning and setting a new body directly.
|
Merging #1978 will fix the php7 failing tests. |
|
Thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Fixes issue #1818. The withJson method now clones the Response, and
updates this with a new header, and a new Body.