Added method for sending JSON responses

pull/11/head
Mike Cao 14 years ago
parent 9471359495
commit 739cdb34d0

@ -451,9 +451,9 @@ class Flight {
* Stops the framework and outputs the current response. * Stops the framework and outputs the current response.
*/ */
public static function _stop() { public static function _stop() {
self::response()-> self::response()
write(ob_get_clean())-> ->write(ob_get_clean())
send(); ->send();
} }
/** /**
@ -463,11 +463,11 @@ class Flight {
* @param int $msg Response text * @param int $msg Response text
*/ */
public static function _halt($code = 200, $text = '') { public static function _halt($code = 200, $text = '') {
self::response(false)-> self::response(false)
status($code)-> ->status($code)
write($text)-> ->write($text)
cache(false)-> ->cache(false)
send(); ->send();
} }
/** /**
@ -476,28 +476,28 @@ class Flight {
* @param object $ex Exception * @param object $ex Exception
*/ */
public static function _error(Exception $e) { public static function _error(Exception $e) {
self::response(false)-> self::response(false)
status(500)-> ->status(500)
write( ->write(
'<h1>500 Internal Server Error</h1>'. '<h1>500 Internal Server Error</h1>'.
'<h3>'.$e->getMessage().'</h3>'. '<h3>'.$e->getMessage().'</h3>'.
'<pre>'.$e->getTraceAsString().'</pre>' '<pre>'.$e->getTraceAsString().'</pre>'
)-> )
send(); ->send();
} }
/** /**
* Sends an HTTP 404 response when a URL is not found. * Sends an HTTP 404 response when a URL is not found.
*/ */
public static function _notFound() { public static function _notFound() {
self::response(false)-> self::response(false)
status(404)-> ->status(404)
write( ->write(
'<h1>404 Not Found</h1>'. '<h1>404 Not Found</h1>'.
'<h3>The page you have requested could not be found.</h3>'. '<h3>The page you have requested could not be found.</h3>'.
str_repeat(' ', 512) str_repeat(' ', 512)
)-> )
send(); ->send();
} }
/** /**
@ -506,11 +506,11 @@ class Flight {
* @param string $url URL * @param string $url URL
*/ */
public static function _redirect($url, $code = 303) { public static function _redirect($url, $code = 303) {
self::response(false)-> self::response(false)
status($code)-> ->status($code)
header('Location', $url)-> ->header('Location', $url)
write($url)-> ->write($url)
send(); ->send();
} }
/** /**
@ -557,6 +557,19 @@ class Flight {
self::halt(304); self::halt(304);
} }
} }
/**
* Sends a JSON response.
*
* @param mixed $data Data to JSON encode
*/
public static function _json($data) {
self::response(false)
->status(200)
->header('Content-Type', 'application/json')
->write(json_encode($data))
->send();
}
} }
// Initialize framework on include // Initialize framework on include

Loading…
Cancel
Save