Skip to content

Instantly share code, notes, and snippets.

@hewerthomn
Created May 9, 2012 10:12
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 hewerthomn/2643533 to your computer and use it in GitHub Desktop.
Save hewerthomn/2643533 to your computer and use it in GitHub Desktop.
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
<?php
// assume autoloader available and configured
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$path = trim($path, "/");
@list($resource, $params) = explode("/", $path, 2);
$resource = ucfirst(strtolower($resource));
$method = strtolower($_SERVER["REQUEST_METHOD"]);
$params = !empty($params) ? explode("/", $params) : array();
if (class_exists($resource)) {
try {
$resource = new $resource($params);
$resource->{$method}();
}
catch (Exception $e) {
header("HTTP/1.1 500 Internal Server Error");
}
}
else {
header("HTTP/1.1 404 File Not Found");
}
HTTP/1.1 405 Method Not Allowed
Allow: GET, POST
<?php
class Restaurant extends Resource
{
public function __construct($params) {
parent::__construct($params);
}
public function get() {
// logic to handle an HTTP GET request goes here
// ...
}
public function post() {
// logic to handle an HTTP POST request goes here
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment