From 4ccfe8c38630e60ae7f969e6f3bb4e5aa3c369ce Mon Sep 17 00:00:00 2001 From: ozh Date: Tue, 29 Oct 2013 19:26:38 +0100 Subject: [PATCH 1/3] JSONP support --- flight/Engine.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/flight/Engine.php b/flight/Engine.php index e32295e..6cf27f5 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -94,7 +94,7 @@ class Engine { // Register framework methods $methods = array( 'start','stop','route','halt','error','notFound', - 'render','redirect','etag','lastModified','json' + 'render','redirect','etag','lastModified','json','jsonp' ); foreach ($methods as $name) { $this->dispatcher->set($name, array($this, '_'.$name)); @@ -105,6 +105,7 @@ class Engine { $this->set('flight.handle_errors', true); $this->set('flight.log_errors', false); $this->set('flight.views.path', './views'); + $this->set('flight.jsonp.callback', 'jsonp'); $initialized = true; } @@ -438,6 +439,21 @@ class Engine { ->write(json_encode($data)) ->send(); } + + /** + * Sends a JSONP response. + * + * @param mixed $data Data to JSON encode + */ + public function _jsonp($data) { + // Get the callback value (eg '?jsonp=my_function') and pad the output + $callback = \Flight::request()->query[ $this->get('flight.jsonp.callback') ]; + $this->response() + ->status(200) + ->header('Content-Type', 'application/javascript') + ->write($callback.'('.json_encode($data).');') + ->send(); + } /** * Handles ETag HTTP caching. From 5e0627f41bd05c410b52885d472d95f19a1ab6e7 Mon Sep 17 00:00:00 2001 From: ozh Date: Tue, 29 Oct 2013 19:32:26 +0100 Subject: [PATCH 2/3] Coding style (same white space as original file) --- flight/Engine.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index 6cf27f5..7c8e55f 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -446,8 +446,8 @@ class Engine { * @param mixed $data Data to JSON encode */ public function _jsonp($data) { - // Get the callback value (eg '?jsonp=my_function') and pad the output - $callback = \Flight::request()->query[ $this->get('flight.jsonp.callback') ]; + // Get the callback value (eg '?jsonp=my_function') and pad the output + $callback = \Flight::request()->query[ $this->get('flight.jsonp.callback') ]; $this->response() ->status(200) ->header('Content-Type', 'application/javascript') From 258e56b4ec7dd51807ca2825ceb74ebbeaab5d55 Mon Sep 17 00:00:00 2001 From: ozh Date: Wed, 30 Oct 2013 09:51:22 +0100 Subject: [PATCH 3/3] Use $this --- flight/Engine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flight/Engine.php b/flight/Engine.php index 7c8e55f..554a93f 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -447,7 +447,7 @@ class Engine { */ public function _jsonp($data) { // Get the callback value (eg '?jsonp=my_function') and pad the output - $callback = \Flight::request()->query[ $this->get('flight.jsonp.callback') ]; + $callback = $this->request()->query[ $this->get('flight.jsonp.callback') ]; $this->response() ->status(200) ->header('Content-Type', 'application/javascript')