Permalink
Browse files

Fixed a bug with my original extension checker for formats.

  • Loading branch information...
1 parent c39dd34 commit 2a5514bd9cb2b1b1570d26e65db25a07c7ab7d87 @BaylorRae committed Feb 19, 2011
Showing with 21 additions and 5 deletions.
  1. +8 −2 index.php
  2. +13 −3 sammy.php
View
@@ -17,7 +17,7 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script>
- $('#results').load('ajax');
+ $('#results').load('/ajax');
</script>
@@ -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() {
View
@@ -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() {

0 comments on commit 2a5514b

Please sign in to comment.