Zend\Diactoros\Response::__construct PHP Method

__construct() public method

public __construct ( string | resource | Psr\Http\Message\StreamInterface $body = 'php://memory', integer $status = 200, array $headers = [] )
$body string | resource | Psr\Http\Message\StreamInterface Stream identifier and/or actual stream resource
$status integer Status code for the response, if any.
$headers array Headers for the response, if any.
    public function __construct($body = 'php://memory', $status = 200, array $headers = [])
    {
        $this->setStatusCode($status);
        $this->stream = $this->getStream($body, 'wb+');
        list($this->headerNames, $headers) = $this->filterHeaders($headers);
        $this->assertHeaders($headers);
        $this->headers = $headers;
    }

Usage Example

Example #1
0
 /**
  * Create a JSON response with the given data.
  *
  * 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
  *
  * @param mixed $data Data to convert to JSON.
  * @param int $status Integer status code for the response; 200 by default.
  * @param array $headers Array of headers to use at initialization.
  * @param int $encodingOptions JSON encoding options to use.
  * @throws InvalidArgumentException if unable to encode the $data to JSON.
  */
 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));
     $headers = $this->injectContentType('application/json', $headers);
     parent::__construct($body, $status, $headers);
 }
All Usage Examples Of Zend\Diactoros\Response::__construct