Skip to content

Instantly share code, notes, and snippets.

@arnold-almeida
Created May 9, 2014 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnold-almeida/2f656202153b3e161bcb to your computer and use it in GitHub Desktop.
Save arnold-almeida/2f656202153b3e161bcb to your computer and use it in GitHub Desktop.
<?php
class JsonpResponseFormat extends \Dingo\Api\Http\ResponseFormat\JsonResponseFormat
{
/**
* Get the response content type.
*
* @return string
*/
public function getContentType()
{
if (isset($_GET['callback']) && !empty($_GET['callback'])) {
return 'application/javascript';
}
return 'application/json';
}
/**
* Encode the content to its JSON representation.
*
* @param string $content
* @return string
*/
protected function encode($content)
{
if (isset($_GET['callback']) && !empty($_GET['callback'])) {
$callback = $_GET['callback'];
return sprintf('%s(%s);', $callback, json_encode($content));
}
return json_encode($content);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment