Skip to content

Commit

Permalink
Added additional options for JSON methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Dec 19, 2013
1 parent 6696c0d commit 6fe4e32
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,12 @@ public function _start() {

/**
* Stops the framework and outputs the current response.
*
* @param int $code HTTP status code
*/
public function _stop() {
public function _stop($code = 200) {
$this->response()
->status($code)
->write(ob_get_clean())
->send();
}
Expand Down Expand Up @@ -430,29 +433,37 @@ public function _render($file, $data = null, $key = null) {
/**
* 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()
->status(200)
->status($code)
->header('Content-Type', 'application/json')
->write(json_encode($data))
->write($json)
->send();
}

/**
* 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) {
// Get the callback value (eg '?jsonp=my_function') and pad the output
$callback = $this->request()->query[ $this->get('flight.jsonp.callback') ];
$this->response()
->status(200)
->header('Content-Type', 'application/javascript')
->write($callback.'('.json_encode($data).');')
->send();
public function _jsonp($data, $code = 200, $encode = true) {
$json = ($encode) ? json_encode($data) : $data;

$callback = $this->request()->query[$this->get('flight.jsonp.callback')];

$this->json(
$callback.'('.$json.');',
$code,
!$encode
);
}

/**
Expand Down

0 comments on commit 6fe4e32

Please sign in to comment.