-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
use Symfony\Component\DependencyInjection\ContainerAwareTrait; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\StreamedResponse; | ||
|
@@ -97,6 +98,29 @@ protected function redirectToRoute($route, array $parameters = array(), $status | |
return $this->redirect($this->generateUrl($route, $parameters), $status); | ||
} | ||
|
||
/** | ||
* Returns a JsonResponse that uses the serializer component if enabled, or json_encode. | ||
* | ||
* @param mixed $data The response data | ||
* @param int $status The status code to use for the Response | ||
* @param array $headers Array of extra headers to add | ||
* @param array $context Context to pass to serializer when using serializer component | ||
* | ||
* @return JsonResponse | ||
*/ | ||
protected function json($data, $status = 200, $headers = array(), $context = array()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was looking at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok :) |
||
{ | ||
if ($this->container->has('serializer')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of having 2 different behaviors depending of the configuration. What do you think about splitting this method in 2 separates methods and throw an error in the method using the serializer if it is not available? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do understand this point, but then again, looking at Also if #17603 is merged the result would be the same after you enable the serializer. |
||
$json = $this->container->get('serializer')->serialize($data, 'json', array_merge(array( | ||
'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS, | ||
), $context)); | ||
|
||
return new JsonResponse($json, $status, $headers, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sorry, I missed the change below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But you now have to increase the required version of |
||
} | ||
|
||
return new JsonResponse($data, $status, $headers); | ||
} | ||
|
||
/** | ||
* Adds a flash message to the current session for type. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
"symfony/dependency-injection": "~2.8|~3.0", | ||
"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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that's right |
||
"symfony/http-kernel": "~2.8|~3.0", | ||
"symfony/polyfill-mbstring": "~1.0", | ||
"symfony/filesystem": "~2.8|~3.0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,24 +29,27 @@ class JsonResponse extends Response | |
|
||
// Encode <, >, ', &, and " for RFC4627-compliant JSON, which may also be embedded into HTML. | ||
// 15 === JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | ||
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 commentThe 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 commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ow, yeah I may have mistaken with arithmetics such as |
||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param mixed $data The response data | ||
* @param int $status The response status code | ||
* @param array $headers An array of response headers | ||
* @param mixed $data The response data | ||
* @param int $status The response status code | ||
* @param array $headers An array of response headers | ||
* @param bool $preEncoded If the data is already a JSON string | ||
*/ | ||
public function __construct($data = null, $status = 200, $headers = array()) | ||
public function __construct($data = null, $status = 200, $headers = array(), $preEncoded = false) | ||
{ | ||
parent::__construct('', $status, $headers); | ||
|
||
if (null === $data) { | ||
$data = new \ArrayObject(); | ||
} | ||
|
||
$this->setData($data); | ||
$this->setData($data, $preEncoded); | ||
} | ||
|
||
/** | ||
|
@@ -88,34 +91,37 @@ public function setCallback($callback = null) | |
* Sets the data to be sent as JSON. | ||
* | ||
* @param mixed $data | ||
* @param bool $preEncoded If the data is already a JSON string | ||
* | ||
* @return JsonResponse | ||
* | ||
* @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 commentThe reason will be displayed to describe this comment to others. Learn more. Breaks BC. |
||
{ | ||
if (defined('HHVM_VERSION')) { | ||
// HHVM does not trigger any warnings and let exceptions | ||
// thrown from a JsonSerializable object pass through. | ||
// If only PHP did the same... | ||
$data = json_encode($data, $this->encodingOptions); | ||
} else { | ||
try { | ||
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable | ||
// objects in a new exception that needs to be removed. | ||
// Fortunately, PHP 5.5 and up do not trigger any warning anymore. | ||
if (!$preEncoded) { | ||
if (defined('HHVM_VERSION')) { | ||
// HHVM does not trigger any warnings and let exceptions | ||
// thrown from a JsonSerializable object pass through. | ||
// If only PHP did the same... | ||
$data = json_encode($data, $this->encodingOptions); | ||
} catch (\Exception $e) { | ||
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) { | ||
throw $e->getPrevious() ?: $e; | ||
} else { | ||
try { | ||
// PHP 5.4 and up wrap exceptions thrown by JsonSerializable | ||
// objects in a new exception that needs to be removed. | ||
// Fortunately, PHP 5.5 and up do not trigger any warning anymore. | ||
$data = json_encode($data, $this->encodingOptions); | ||
} catch (\Exception $e) { | ||
if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) { | ||
throw $e->getPrevious() ?: $e; | ||
} | ||
throw $e; | ||
} | ||
throw $e; | ||
} | ||
} | ||
|
||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new \InvalidArgumentException(json_last_error_msg()); | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
throw new \InvalidArgumentException(json_last_error_msg()); | ||
} | ||
} | ||
|
||
$this->data = $data; | ||
|
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.