1. Send Everything to index.php using .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
2. Figure Out What Was Requested
class Request
{
public $url_elements;
public $verb;
public $parameters;
public function __construct()
{
$this->verb = $_SERVER['REQUEST_METHOD'];
$this->url_elements = explode('/', $_SERVER['PATH_INFO']);
<continued below in Step 3>
3. Process Incoming Data according to format
<continued from Step 2>
$this->parseIncomingParams();
// initialise json as default format
$this->format = 'json';
if(isset($this->parameters['format']))
{
$this->format = $this->parameters['format'];
}
return true;
} //end of public function __construct()
public function parseIncomingParams()
{
$parameters = array();
// first of all, retrieve the GET vars
if (isset($_SERVER['QUERY_STRING']))
{
parse_str($_SERVER['QUERY_STRING'], $parameters);
}
// now how about PUT/POST bodies? These override what we got from GET
$body = file_get_contents("php://input");
$content_type = false;
if(isset($_SERVER['CONTENT_TYPE']))
{
$content_type = $_SERVER['CONTENT_TYPE'];
}
switch($content_type)
{
case "application/json":
$body_params = json_decode($body);
if($body_params)
{
foreach($body_params as $param_name => $param_value)
{
$parameters[$param_name] = $param_value;
}
}
$this->format = "json";
break;
case "application/x-www-form-urlencoded":
parse_str($body, $postvars);
foreach($postvars as $field => $value)
{
$parameters[$field] = $value;
}
$this->format = "html";
break;
default:
// we could parse other supported formats here
break;
} //end of switch($content_type)
$this->parameters = $parameters;
} // end of public function parseIncomingParams()
} // end of class Request
No comments:
Post a Comment