Created
December 15, 2010 10:45
-
-
Save havvg/741852 to your computer and use it in GitHub Desktop.
A simple JSONP filter for symfony web applications.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class JsonpFilter extends sfFilter | |
{ | |
public function execute($filterChain) | |
{ | |
// nothing to do before the action is called | |
$filterChain->execute(); | |
if (($response = $this->getContext()->getResponse()) instanceof sfWebResponse and ($request = $this->getContext()->getRequest()) instanceof sfWebRequest) | |
{ | |
/* @var $request sfWebRequest */ | |
if ($request->getParameter('sf_format') === 'json' and $callback = $request->getGetParameter('callback')) | |
{ | |
/* @var $response sfWebResponse */ | |
$response->setContentType('text/javascript'); | |
$response->setContent(sprintf('%s(%s);', $callback, $response->getContent())); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment