Cake\Network\Response::__construct PHP Method

__construct() public method

Constructor
public __construct ( array $options = [] )
$options array list of parameters to setup the response. Possible values are: - body: the response text that should be sent to the client - statusCodes: additional allowable response codes - status: the HTTP status code to respond with - type: a complete mime-type string or an extension mapped in this class - charset: the charset for the response body
    public function __construct(array $options = [])
    {
        if (isset($options['body'])) {
            $this->body($options['body']);
        }
        if (isset($options['statusCodes'])) {
            $this->httpCodes($options['statusCodes']);
        }
        if (isset($options['status'])) {
            $this->statusCode($options['status']);
        }
        if (isset($options['type'])) {
            $this->type($options['type']);
        }
        if (!isset($options['charset'])) {
            $options['charset'] = Configure::read('App.encoding');
        }
        $this->charset($options['charset']);
    }

Usage Example

 /**
  * Constructor
  *
  * @param string $code 	One of Types::CODE_*, or an array containing 'code' and 'data' keys
  * @param array $data 	data to return
  */
 public function __construct($code, array $data = array())
 {
     if (is_array($code)) {
         $body = \Cake\Utility\Hash::merge(array('code' => 'success', 'data' => array()), $code);
     } else {
         $body = array('code' => $code, 'data' => $data);
     }
     $options = array('type' => 'json', 'body' => json_encode($body));
     parent::__construct($options);
 }