File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments