Added additional options for JSON methods.

pull/57/merge
Mike Cao 11 years ago
parent 6696c0d267
commit 6fe4e32fee

@ -317,9 +317,12 @@ class Engine {
/** /**
* Stops the framework and outputs the current response. * Stops the framework and outputs the current response.
*
* @param int $code HTTP status code
*/ */
public function _stop() { public function _stop($code = 200) {
$this->response() $this->response()
->status($code)
->write(ob_get_clean()) ->write(ob_get_clean())
->send(); ->send();
} }
@ -430,29 +433,37 @@ class Engine {
/** /**
* Sends a JSON response. * Sends a JSON response.
* *
* @param mixed $data Data to JSON encode * @param mixed $data JSON data
* @param int $code HTTP status code
* @param bool $encode Whether to perform JSON encoding
*/ */
public function _json($data) { public function _json($data, $code = 200, $encode = true) {
$json = ($encode) ? json_encode($data) : $data;
$this->response() $this->response()
->status(200) ->status($code)
->header('Content-Type', 'application/json') ->header('Content-Type', 'application/json')
->write(json_encode($data)) ->write($json)
->send(); ->send();
} }
/** /**
* Sends a JSONP response. * Sends a JSONP response.
* *
* @param mixed $data Data to JSON encode * @param mixed $data JSON data
* @param int $code HTTP status code
* @param bool $encode Whether to perform JSON encoding
*/ */
public function _jsonp($data) { public function _jsonp($data, $code = 200, $encode = true) {
// Get the callback value (eg '?jsonp=my_function') and pad the output $json = ($encode) ? json_encode($data) : $data;
$callback = $this->request()->query[$this->get('flight.jsonp.callback')]; $callback = $this->request()->query[$this->get('flight.jsonp.callback')];
$this->response()
->status(200) $this->json(
->header('Content-Type', 'application/javascript') $callback.'('.$json.');',
->write($callback.'('.json_encode($data).');') $code,
->send(); !$encode
);
} }
/** /**

Loading…
Cancel
Save