File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-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+ * @author Tom van Oorschot <[email protected] > 6+ * @since 17-12-2012
7+ *
8+ * Simple class to wrap the response of the application in a JSONP callback function.
9+ * The class is triggered when a get parameter of callback is found
10+ *
11+ * Usage
12+ * ====
13+ *
14+ * $app = new \Slim\Slim();
15+ * $app->add(new \Slim\Extras\Middleware\JSONPMiddleware());
16+ *
17+ */
18+
19+ namespace Slim \Extras \Middleware ;
20+
21+ class JSONPMiddleware extends \Slim \Middleware
22+ {
23+ public function call ()
24+ {
25+ $ callback = $ this ->app ->request ()->get ('callback ' );
26+
27+ //Fetch the body first
28+ $ this ->next ->call ();
29+
30+ //If the JSONP callback parameter is set then wrap the response body in the original
31+ //callback string.
32+ if (!empty ($ callback )){
33+ //The response becomes a javascript response
34+ $ this ->app ->contentType ('application/javascript ' );
35+
36+ $ jsonp_response = htmlspecialchars ($ callback ) . "( " .$ this ->app ->response ()->body () . ") " ;
37+ $ this ->app ->response ()->body ($ jsonp_response );
38+ }
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments