Symfony\Bundle\FrameworkBundle\Controller\Controller::json PHP Method

json() protected method

Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
protected json ( mixed $data, integer $status = 200, array $headers = [], array $context = [] ) : Symfony\Component\HttpFoundation\JsonResponse
$data mixed The response data
$status integer The status code to use for the Response
$headers array Array of extra headers to add
$context array Context to pass to serializer when using serializer component
return Symfony\Component\HttpFoundation\JsonResponse
    protected function json($data, $status = 200, $headers = array(), $context = array())
    {
        if ($this->container->has('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);
        }
        return new JsonResponse($data, $status, $headers);
    }

Usage Example

示例#1
0
 public function json($data, $status = 200, $headers = array(), $context = array())
 {
     return parent::json($data, $status, $headers, $context);
 }