Zend\Diactoros\Response\JsonResponse::__construct PHP Method

__construct() public method

Default JSON encoding is performed with the following options, which produces RFC4627-compliant JSON, capable of embedding into HTML. - JSON_HEX_TAG - JSON_HEX_APOS - JSON_HEX_AMP - JSON_HEX_QUOT - JSON_UNESCAPED_SLASHES
public __construct ( mixed $data, integer $status = 200, array $headers = [], integer $encodingOptions = self::DEFAULT_JSON_FLAGS )
$data mixed Data to convert to JSON.
$status integer Integer status code for the response; 200 by default.
$headers array Array of headers to use at initialization.
$encodingOptions integer JSON encoding options to use.
    public function __construct($data, $status = 200, array $headers = [], $encodingOptions = self::DEFAULT_JSON_FLAGS)
    {
        $body = new Stream('php://temp', 'wb+');
        $body->write($this->jsonEncode($data, $encodingOptions));
        $body->rewind();
        $headers = $this->injectContentType('application/json', $headers);
        parent::__construct($body, $status, $headers);
    }

Usage Example

Example #1
0
 /**
  * {@inheritdoc}
  */
 public function __construct(Document $document, $status = 200, array $headers = [], $encodingOptions = 15)
 {
     $headers['content-type'] = 'application/vnd.api+json';
     // The call to jsonSerialize prevents rare issues with json_encode() failing with a
     // syntax error even though Document implements the JsonSerializable interface.
     // See https://github.com/flarum/core/issues/685
     parent::__construct($document->jsonSerialize(), $status, $headers, $encodingOptions);
 }
All Usage Examples Of Zend\Diactoros\Response\JsonResponse::__construct