-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathJSONPMiddleware.php
40 lines (35 loc) · 1.09 KB
/
JSONPMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
/**
* JSONP Middleware Class for the Slim Framework
*
* @author Tom van Oorschot <[email protected]>
* @since 17-12-2012
*
* Simple class to wrap the response of the application in a JSONP callback function.
* The class is triggered when a get parameter of callback is found
*
* Usage
* ====
*
* $app = new \Slim\Slim();
* $app->add(new \Slim\Extras\Middleware\JSONPMiddleware());
*
*/
namespace Slim\Extras\Middleware;
class JSONPMiddleware extends \Slim\Middleware
{
public function call()
{
$callback = $this->app->request()->get('callback');
//Fetch the body first
$this->next->call();
//If the JSONP callback parameter is set then wrap the response body in the original
//callback string.
if(!empty($callback)){
//The response becomes a javascript response
$this->app->contentType('application/javascript');
$jsonp_response = htmlspecialchars($callback) . "(" .$this->app->response()->body() . ")";
$this->app->response()->body($jsonp_response);
}
}
}