-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[FrameworkBundle] [DX] Add Controller::json method to make it easy to send json
#17642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
0ccff59 to
fe42ab8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is broken in case you call the setter after setting data
|
Why not something like https://github.com/dunglas/DunglasApiBundle/blob/1.x/Controller/ResourceController.php#L79-L93? I agree with @stof, |
fe42ab8 to
11bc151
Compare
|
Now I look at it again, I completely agree on the weirdness of inserting the Serializer into the Response. How about what I have done now? Its very similar to how DunglasApiBundle does it, but keeping the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about renderJson or something similar to existing methods?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json is consistent with stream to return a stream response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was looking at redirect and stream as similar names, also renderView for example returns a string, not a Response
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok :)
|
@dunglas That was a long read! Would certainly wait until a decision is made there. I see some real value in breaking up an every growing Controller class. |
273a896 to
ecd3587
Compare
|
LGTM. ping @symfony/deciders |
| $json = $this->container->get('serializer')->serialize($data, 'json', array_merge(array( | ||
| 'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS, | ||
| ), $context)); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fourth argument should be removed.
Sorry, I missed the change below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you now have to increase the required version of symfony/http-foundation.
ecd3587 to
c37096a
Compare
| "symfony/config": "~2.8|~3.0", | ||
| "symfony/event-dispatcher": "~2.8|~3.0", | ||
| "symfony/http-foundation": "~2.8|~3.0", | ||
| "symfony/http-foundation": "~3.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xabbuh is this the best way to do it? I see at least one other component has a ~3.0 requirement, I assume this is expected to change as new functionality is added to Symfony 3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that's right
|
@xabbuh Does anything else need to be done? |
c37096a to
d4511eb
Compare
| protected $encodingOptions = 15; | ||
| const DEFAULT_ENCODING_OPTIONS = 15; | ||
|
|
||
| protected $encodingOptions = self::DEFAULT_ENCODING_OPTIONS; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IRC, the possibility to assign a property from a constant value is available only on php >= 5.6, and Sf3 is php >= 5.5...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell this usage has always been part of PHP 5. It certainly works in 5.5. You made me doubt it and I ran the tests for this class in 5.5 to double check!
I know that from PHP 7 its possible to write this how I would have really liked
const DEFAULT_ENCODING_OPTIONS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ow, yeah I may have mistaken with arithmetics such as const DEFAULT_ENCODING_OPTIONS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT indeed. My bad !
|
👍 |
|
@xabbuh does the 'needs work' label still need to be here? |
|
@mcfedr No, you're right. Status: Reviewed |
| 3.1.0 | ||
| ----- | ||
|
|
||
| * Added `Controller:json` to simplify creating JSON responses when using the Serializer component |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: missing double colon
If the serializer component is enabled it is used to generate the json data, if not the standard `json_encode` function is used
d4511eb to
f904a2b
Compare
|
Thank you @mcfedr. |
…o make it easy to send json (mcfedr) This PR was merged into the 3.1-dev branch. Discussion ---------- [FrameworkBundle] [DX] Add `Controller::json` method to make it easy to send json | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Its currently a awkward to use Serializer component to send a `JsonResponse`. I have tried two approaches 1. use `Serializer::normalize` and `JsonResponse` 1. use `Serializer::serialize` and a plain `Response`, and set the `content-type` In either cases there is need for a custom `json` function so as not to repeat yourself and there are disadvantages. 1. In the first case you are only partly using `Serializer` and any custom `Encoder` would be skipped 1. In the second you are not making use of `JsonResponse`, particular disadvantage if you want to support JSONP. This new `json` method uses the serializer component is enabled it is used to generate the json data, and falls back to normal `JsonResponse` when its not. Commits ------- f904a2b Add a Controller function to make it easy to return json
|
This breaks BC by adding a new property to public |
| * @throws \InvalidArgumentException | ||
| */ | ||
| public function setData($data = array()) | ||
| public function setData($data = array(), $preEncoded = false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaks BC.
|
I agree - it looks like this violates our BC policy - http://symfony.com/doc/current/contributing/code/bc.html#changing-classes - sub-item "Public Methods -> Add argument with a default value". I don't think we allow a change like this. |
|
What we can do instead is adding a new public setPreEncoded method. |
|
Or "setJson" :-) which should be called by setData internally btw |
|
I think the |
This PR was squashed before being merged into the 3.1-dev branch (closes #18006). Discussion ---------- [HttpFoundation] Fix BC break introduced by #17642 | Q | A | ------------- | --- | Branch | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17642 | License | MIT | Doc PR | n/a @taylorotwell can you confirm it fixes the issue with Laravel? Commits ------- 73ef995 [HttpFoundation] Fix BC break introduced by #17642
| 3.1.0 | ||
| ----- | ||
|
|
||
| * Added `Controller::json` to simplify creating JSON responses when using the Serializer component |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing [BC break] ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, it's not a BC break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, sorry I got confused by #18373.
…n() method (derrabus) This PR was merged into the 3.1-dev branch. Discussion ---------- [FrameworkBundle] Upgrade notice for the Controller::json() method | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A PR #17642 added a `json()` method to the `Controller` class. This might break existing code extending the class. This PR adds a note about this to the UPGRADE-3.1 document. Commits ------- ca6694a Upgrade notice for the Controller::json() method.
|
Awsome! |
|
nice job, Fred! |
Its currently a awkward to use Serializer component to send a
JsonResponse.I have tried two approaches
Serializer::normalizeandJsonResponseSerializer::serializeand a plainResponse, and set thecontent-typeIn either cases there is need for a custom
jsonfunction so as not to repeat yourself and there are disadvantages.Serializerand any customEncoderwould be skippedJsonResponse, particular disadvantage if you want to support JSONP.This new
jsonmethod uses the serializer component is enabled it is used to generate the json data, and falls back to normalJsonResponsewhen its not.