Skip to content

Instantly share code, notes, and snippets.

@robertpitt
Created January 11, 2015 22:11
Show Gist options
  • Save robertpitt/421ca3efabe5817b1d11 to your computer and use it in GitHub Desktop.
Save robertpitt/421ca3efabe5817b1d11 to your computer and use it in GitHub Desktop.
<?php
/**
* LightPHP Framework
* LitePHP is a framework that has been designed to be lite waight, extensible and fast.
*
* @author Robert Pitt <[email protected]>
* @category core
* @copyright 2013 Robert Pitt
* @license GPL v3 - GNU Public License v3
* @version 1.0.0
*/
!defined('SECURE') && die('Access Forbidden!');
/**
* Load the base API Controller
*/
require_once Registry::get('Application')->getResourceLocation('includes/controllers', 'api', 'php');
/**
* Access Oauth infomration related to the token
*/
class Sample_Controller extends APIController
{
/**
*/
public function get_info()
{
}
/**
*/
public function post_info()
{
}
/**
*/
public function put_info()
{
}
<?php
/**
* Set the response type to json
*/
header("Content-Type: application/json");
$payload = array(
"code" => $context->getCode(),
"error" => "error",
"error_description" => $context->getMessage()
);
/**
* If we are in development mode
*/
if(ENVIRONMENT != 'production')
{
$payload['line'] = $context->getLine();
$payload['file'] = $context->getFile();
$payload['trace'] = $context->getTraceAsString();
}
/**
* Send the code and message
*/
echo json_encode($payload);
<?php
/**
* LightPHP Framework
* LitePHP is a framework that has been designed to be lite waight, extensible and fast.
*
* @author Robert Pitt <[email protected]>
* @category core
* @copyright 2013 Robert Pitt
* @license GPL v3 - GNU Public License v3
* @version 1.0.0
*/
!defined('SECURE') && die('Access Forbidden!');
/**
* Homepage controller
*/
class APIController extends Controller
{
/**
* Main cosntructor
*/
public function __construct()
{
/**
* [$method description]
* @var [type]
*/
$method = strtolower($this->input->getRequestMethod());
/**
* Fetch the controller name
* @var String
*/
$controller = Registry::get("Route")->getController();
/**
* Fetch the function name that is being called
* @var String
*/
$func = Registry::get("Route")->getMethod();
/**
* Set the method
*/
Registry::get("Route")->setMethod($method . "_" . $func);
/**
* Add the CORS Header in.
*/
$this->output->setHeader("Access-Control-Allow-Origin", "*");
}
/**
* Send an entity back to the client
* @param Array|Object|String $data
* @param integer $status status code to responde with, defualts to 200 OK.
*/
protected function send($data, $status = 200)
{
/**
* Set the status of header
*/
$this->output->setStatus($status);
/**
* Actually send the data
*/
$this->output->sendJSON($data);
/**
* Exit the response here as we only send a JSON object on the api
*/
exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment