From 13638467b4d1f48c867cc74dc71bc59f729a6f9f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 2 May 2012 08:35:26 -0500 Subject: [PATCH] added Response::json method. --- laravel/documentation/changes.md | 1 + laravel/response.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/laravel/documentation/changes.md b/laravel/documentation/changes.md index 7e5ef2bc..a7c6724e 100644 --- a/laravel/documentation/changes.md +++ b/laravel/documentation/changes.md @@ -68,6 +68,7 @@ - Allow the registration of custom database drivers. - New, driver based authentication system. - Added Input::json() method for working with applications using Backbone.js or similar. +- Added Response::json method for creating JSON responses. ## Upgrading From 3.1 diff --git a/laravel/response.php b/laravel/response.php index 49c8fda0..acca8a37 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -78,6 +78,26 @@ class Response { return new static(View::make($view, $data)); } + /** + * Create a new JSON response. + * + * + * // Create a response instance with a view + * return Response::json($data, 200, array('header' => 'value')); + * + * + * @param mixed $data + * @param int $status + * @param array $headers + * @return Response + */ + public static function json($data, $status = 200, $headers = array()) + { + $headers['Content-Type'] = 'application/json'; + + return new static(json_encode($data), $status, $headers); + } + /** * Create a new error response instance. *