Skip to content

Instantly share code, notes, and snippets.

@thatisuday
Last active May 10, 2016 11:30
Show Gist options
  • Save thatisuday/98556728fe8f7242bd1dde0711786120 to your computer and use it in GitHub Desktop.
Save thatisuday/98556728fe8f7242bd1dde0711786120 to your computer and use it in GitHub Desktop.
Simple PHP REST Router for MVC Frameworks
<?php
/*
* REST INTERNAL API
* © Uday Hiwarale
* Date Created : 10/05/2016
* API Version : 1.0
**/
/*
* Set headers
**/
header('Content-Type: application/json');
http_response_code(400); //Bad Request
/*
* Start autoloading
**/
require_once('autoload.php');
/*
* Allow only HTTPS connections
**/
if(empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == 'off'){
exit;
}
/*
* Get HTTP Method
**/
$httpMethod = strtolower($_SERVER['REQUEST_METHOD']);
$validHttpMethods = array('get', 'post', 'put', 'patch', 'delete');
if(!in_array($httpMethod, $validHttpMethods)){
http_response_code(405); //Method Not Allowed
exit;
}
/*
* Get headers
**/
$authToken = (!empty($_SERVER['HTTP_X_AUTH_TOKEN'])) ? $_SERVER['HTTP_X_AUTH_TOKEN'] : '';
$apiVersion = (!empty($_SERVER['HTTP_X_API_VERSION'])) ? $_SERVER['HTTP_X_API_VERSION'] : 'v1';
/*
* Check sign in authorization
**/
$authHttpMethods = array('post', 'put', 'patch', 'delete');
if(in_array($httpMethod, $authHttpMethods)){
if(empty($authToken)){
http_response_code(401); //Unauthorized
exit;
}
else{
$mongo = new \MongoClient();
$found = $mongo -> archibiz -> users -> findOne(
array('authToken' => $authToken)
);
if(empty($found)){
http_response_code(403); //Forbidden
exit;
}
}
}
/*
* Gather URL Parameters
**/
$routeUrl = explode("?", $_SERVER['REQUEST_URI'], 2)[0];
$routeUrl = trim($routeUrl, '/');
$routeUrlArr = (empty($routeUrl)) ? array() : explode('/', $routeUrl);
/*
* Get class and method name for routeUrl
**/
if(empty($routeUrlArr[0]) || empty($routeUrlArr[1])){
exit;
}
else{
$class = $routeUrlArr[0];
$method = $routeUrlArr[1];
}
/*
* Get request body
**/
$routeParams = array_slice($routeUrlArr, 2);
$reqBody = (!empty(file_get_contents('php://input'))) ? json_decode(file_get_contents('php://input'), true) : array();
/*
* Create object for REST Request
* Call requested method
**/
try{
//Prepare class name
$class = "api\\{$apiVersion}\\{$httpMethod}\\{$class}";
//Create class object
if($httpMethod == 'get'){
$getParams = (!empty($_GET)) ? $_GET : array();
$obj = new $class($routeParams, $getParams);
$res = $obj -> $method();
if(!empty($res)){
echo json_encode($res);
http_response_code(200); //Ok
}
else{
http_response_code(204); //No content
}
}
else if($httpMethod == 'post'){
$obj = new $class($routeParams, $reqBody);
$res = $obj -> $method();
}
else if($httpMethod == 'put'){
$obj = new $class($routeParams, $reqBody);
$res = $obj -> $method();
}
else if($httpMethod == 'patch'){
$obj = new $class($routeParams, $reqBody);
$res = $obj -> $method();
}
else if($httpMethod == 'delete'){
$obj = new $class($routeParams);
$res = $obj -> $method();
}
}
catch(\Exception $e){
echo $e -> getMessage();
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment