From b64b32949839e42c5c8c717e9d0fbbe22206f07b Mon Sep 17 00:00:00 2001 From: Montana Flynn Date: Sun, 10 Mar 2013 17:11:01 -0700 Subject: [PATCH 1/3] Add API middleware --- Middleware/api.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Middleware/api.php diff --git a/Middleware/api.php b/Middleware/api.php new file mode 100644 index 0000000..1e85d6f --- /dev/null +++ b/Middleware/api.php @@ -0,0 +1,58 @@ + + * @since 3/10/13 + * + * Simple class to make building API's easier + * + * Usage + * ==== + * + * $api = new \Slim\slim(); + * $api->add(new \Slim\Extras\Middleware\API()); + * + */ + +namespace Slim\Extras\Middleware; + +class API extends \Slim\Middleware +{ + public function call() + { + + // Just to make things easy, we can avoid the 404 page and override with + // helpful error messages. May extend later to find all registered endpoints + $app = $this->app; + + // Change to json + $response = $app->response(); + $response['Content-Type'] = 'application/json'; + + // No Endpoint Specified? + $app->get('/', function() use ($app) { + $app->halt(400, json_encode(array('error'=>'You must specify an endpoint!'))); + }); + + // Cannot Find Endpoint? + $app->get('/:method', function($method) use ($app) { + $app->halt(400, json_encode(array('error'=>'There is no endpoint named '.$method.'!'))); + })->conditions(array('method' => '.+')); + + // Output jsonp if user sets a callback parameter + // Tom van Oorschot + $request = $app->request(); + $callback = $request->params('callback'); + + // Get the next response so we can wrap it + $this->next->call(); + + // Do the damn thing + if(!empty($callback)){ + $this->app->contentType('application/javascript'); + $jsonp_response = htmlspecialchars($callback) . "(" .$this->app->response()->body() . ")"; + $this->app->response()->body($jsonp_response); + } + } +} \ No newline at end of file From f7f3d518af5bf6983c92431771d48261f329d3bb Mon Sep 17 00:00:00 2001 From: Montana Flynn Date: Sun, 10 Mar 2013 17:25:29 -0700 Subject: [PATCH 2/3] Refactor and commenting --- Middleware/api.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Middleware/api.php b/Middleware/api.php index 1e85d6f..b6617af 100644 --- a/Middleware/api.php +++ b/Middleware/api.php @@ -39,16 +39,15 @@ public function call() $app->get('/:method', function($method) use ($app) { $app->halt(400, json_encode(array('error'=>'There is no endpoint named '.$method.'!'))); })->conditions(array('method' => '.+')); - - // Output jsonp if user sets a callback parameter - // Tom van Oorschot - $request = $app->request(); - $callback = $request->params('callback'); - // Get the next response so we can wrap it + // Move along to next call $this->next->call(); + + // But wait! Let's add support for jsonp callbacks + // Stolen from Tom van Oorschot + $request = $app->request(); + $callback = $request->params('callback'); - // Do the damn thing if(!empty($callback)){ $this->app->contentType('application/javascript'); $jsonp_response = htmlspecialchars($callback) . "(" .$this->app->response()->body() . ")"; From 359d41217d9f6441764cf439195b99917643208e Mon Sep 17 00:00:00 2001 From: Montana Flynn Date: Sun, 10 Mar 2013 17:32:16 -0700 Subject: [PATCH 3/3] Minor refactoring of jsonp callback code originally written by Tom van Oorschot --- Middleware/api.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Middleware/api.php b/Middleware/api.php index b6617af..b2c14b6 100644 --- a/Middleware/api.php +++ b/Middleware/api.php @@ -44,14 +44,13 @@ public function call() $this->next->call(); // But wait! Let's add support for jsonp callbacks - // Stolen from Tom van Oorschot $request = $app->request(); $callback = $request->params('callback'); if(!empty($callback)){ - $this->app->contentType('application/javascript'); - $jsonp_response = htmlspecialchars($callback) . "(" .$this->app->response()->body() . ")"; - $this->app->response()->body($jsonp_response); + $app->contentType('application/javascript'); + $jsonp_response = $callback . "(" .$app->response()->body() . ")"; + $app->response()->body($jsonp_response); } } } \ No newline at end of file