Skip to content

Instantly share code, notes, and snippets.

@havvg
Created December 15, 2010 10:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havvg/741852 to your computer and use it in GitHub Desktop.
Save havvg/741852 to your computer and use it in GitHub Desktop.
A simple JSONP filter for symfony web applications.
<?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