Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from jk/master

Merging some fixes, dot syntax for format, and .htaccess sample from Jens Kohl.
  • Loading branch information...
commit 2b96fb305e9a268c0581519cabd7dc54badb1b8b 2 parents 3710dbb + 6ddb443
@jacwright authored
Showing with 28 additions and 2 deletions.
  1. +20 −2 RestServer.php
  2. +8 −0 sample.htaccess
View
22 RestServer.php
@@ -35,6 +35,7 @@ class RestFormat
const JSON = 'application/json';
static public $formats = array(
'plain' => RestFormat::PLAIN,
+ 'txt' => RestFormat::PLAIN,
'html' => RestFormat::HTML,
'amf' => RestFormat::AMF,
'json' => RestFormat::JSON,
@@ -325,6 +326,8 @@ public function getPath()
}
// remove root from path
if ($this->root) $path = str_replace($this->root, '', $path);
+ // remove trailing format definition, like /controller/action.json -> /controller/action
+ $path = preg_replace('/\.(\w+)$/i', '', $path);
return $path;
}
@@ -343,8 +346,23 @@ public function getMethod()
public function getFormat()
{
$format = RestFormat::PLAIN;
- $accept = explode(',', $_SERVER['HTTP_ACCEPT']);
- $override = isset($_GET['format']) ? $_GET['format'] : '';
+ $accept_mod = preg_replace('/\s+/i', '', $_SERVER['HTTP_ACCEPT']); // ensures that exploding the HTTP_ACCEPT string does not get confused by whitespaces
+ $accept = explode(',', $accept_mod);
+
+ if (isset($_REQUEST['format']) || isset($_SERVER['HTTP_FORMAT'])) {
+ // give GET/POST precedence over HTTP request headers
+ $override = isset($_SERVER['HTTP_FORMAT']) ? $_SERVER['HTTP_FORMAT'] : '';
+ $override = isset($_REQUEST['format']) ? $_REQUEST['format'] : $override;
+ $override = trim($override);
+ }
+
+ // Check for trailing dot-format syntax like /controller/action.format -> action.json
+ if(preg_match('/\.(\w+)$/i', $_SERVER['REQUEST_URI'], &$matches)) {
+ $override = $matches[1];
+ }
+
+ // Give GET parameters precedence before all other options to alter the format
+ $override = isset($_GET['format']) ? $_GET['format'] : $override;
if (isset(RestFormat::$formats[$override])) {
$format = RestFormat::$formats[$override];
} elseif (in_array(RestFormat::AMF, $accept)) {
View
8 sample.htaccess
@@ -0,0 +1,8 @@
+# Apache needs to let you overrite this (AllowOverride Indexes or AllowOverride All)
+DirectoryIndex index.php
+<IfModule mod_rewrite.c>
+ # Turn Rewrite Engine on
+ RewriteEngine On
+ # Send all requests to index.php (index.php will parse the request url and routes accordingly)
+ RewriteRule ^.*$ index.php [QSA,L]
+</IfModule>
Please sign in to comment.
Something went wrong with that request. Please try again.