Skip to content

Commit f108da8

Browse files
committed
A simple class to wrap the response of the application in an JSONP callback function.
1 parent a831f4e commit f108da8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Middleware/Jsonp.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* JSONP Middleware Class for the Slim Framework
4+
*
5+
* Simple class to wrap the response of the application in an JSONP callback function.
6+
* The class is triggered when a get parameter of callback is found
7+
*
8+
* @author Tom van Oorschot <[email protected]>
9+
* @since 17-12-2012
10+
*/
11+
class JSONPMiddleware extends \Slim\Middleware
12+
{
13+
public function call()
14+
{
15+
$callback = $this->app->request()->get('callback');
16+
17+
//Fetch the body first
18+
$this->next->call();
19+
20+
//If the JSONP callback parameter is set then wrap the response body in the original
21+
//callback string.
22+
if(!empty($callback)){
23+
//The response becomes a javascript response
24+
$this->app->contentType('application/javascript');
25+
26+
$jsonp_response = htmlspecialchars($callback) . "(" .$this->app->response()->body() . ")";
27+
$this->app->response()->body($jsonp_response);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)