From 2a5514bd9cb2b1b1570d26e65db25a07c7ab7d87 Mon Sep 17 00:00:00 2001 From: BaylorRae Date: Sat, 19 Feb 2011 00:45:57 -0600 Subject: [PATCH] Fixed a bug with my original extension checker for formats. --- index.php | 10 ++++++++-- sammy.php | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/index.php b/index.php index cfbcf2b..0623f39 100755 --- a/index.php +++ b/index.php @@ -17,7 +17,7 @@ @@ -47,7 +47,13 @@ }); get('/hello/(.*?)', function($sammy) { - return 'Hello '.$sammy->segment(2); + + $sammy->format('json', function($sammy) { + return json_encode(array('name', $sammy->segment(2))); + }); + + if( !$sammy->format ) + return 'Hello '.$sammy->segment(2); }); ajax('/ajax', function() { diff --git a/sammy.php b/sammy.php index d70f17b..9a56d3a 100755 --- a/sammy.php +++ b/sammy.php @@ -68,8 +68,13 @@ public static function process($route, $callback, $type) { if( static::$route_found || (!preg_match('@^'.$route.'(?:\.(\w+))?$@uD', $sammy->uri, $matches) || $sammy->method != $type) ) { return false; } - - $sammy->format = (!empty($matches[1])) ? $matches[1] : null; + + // Get the extension + $extension = $matches[count($matches)-1]; + $extension_test = substr($sammy->uri, -(strlen($extension)+1), (strlen($extension)+1)); + + if( $extension_test == '.' . $extension ) + $sammy->format = $extension; static::$route_found = true; echo $callback($sammy); @@ -83,7 +88,12 @@ public function __construct() { } public function segment($num) { - return isset($this->segments[$num - 1]) ? $this->segments[$num - 1] : null; + $num--; + + // Remove the extension + $this->segments[$num] = isset($this->segments[$num]) ? rtrim($this->segments[$num], '.' . $this->format) : null; + + return isset($this->segments[$num]) ? $this->segments[$num] : null; } protected function get_method() {