Skip to content

Instantly share code, notes, and snippets.

@jhuriez
Created August 2, 2013 12:51
Show Gist options
  • Save jhuriez/6139643 to your computer and use it in GitHub Desktop.
Save jhuriez/6139643 to your computer and use it in GitHub Desktop.
<?php
class Jason {
public static function output($status = 'success', $message = '', $data = array())
{
$output = (object) array(
'status' => $status,
'message' => $message,
'data' => $data
);
// Get the active response object
$response = Request::active()->controller_instance->response;
// If this is an ajax request, send it as text/json content
if (Input::is_ajax())
{
$response->set_header('Content-type', 'application/json');
}
return $response->body(json_encode($output));
}
public static function error($message = '', $data = array())
{
if (is_object($message) and is_a($message, 'Exception'))
{
$data = (object) array(
'code'=> $message->getCode(),
'file'=> $message->getFile(),
'line'=> $message->getLine()
);
$message = $message->getMessage();
}
return static::output('error', $message, $data);
}
public static function data($data)
{
return static::output('success', NULL, $data);
}
public static function success($message = '')
{
return static::output('success', $message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment