Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed a bug with my original extension checker for formats.

  • Loading branch information...
commit 2a5514bd9cb2b1b1570d26e65db25a07c7ab7d87 1 parent c39dd34
Baylor Rae' authored February 19, 2011

Showing 2 changed files with 21 additions and 5 deletions. Show diff stats Hide diff stats

  1. 10  index.php
  2. 16  sammy.php
10  index.php
@@ -17,7 +17,7 @@
17 17
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
18 18
     <script>
19 19
 
20  
-      $('#results').load('ajax');
  20
+      $('#results').load('/ajax');
21 21
 
22 22
     </script>
23 23
 
@@ -47,7 +47,13 @@
47 47
 });
48 48
 
49 49
 get('/hello/(.*?)', function($sammy) {
50  
-	return 'Hello '.$sammy->segment(2);
  50
+  
  51
+  $sammy->format('json', function($sammy) {
  52
+    return json_encode(array('name', $sammy->segment(2)));
  53
+  });
  54
+  
  55
+  if( !$sammy->format )
  56
+  	return 'Hello '.$sammy->segment(2);
51 57
 });
52 58
 
53 59
 ajax('/ajax', function() {
16  sammy.php
@@ -68,8 +68,13 @@ public static function process($route, $callback, $type) {
68 68
 		if( static::$route_found || (!preg_match('@^'.$route.'(?:\.(\w+))?$@uD', $sammy->uri, $matches) || $sammy->method != $type) ) {
69 69
 			return false;
70 70
 		}
71  
-				
72  
-		$sammy->format = (!empty($matches[1])) ? $matches[1] : null;
  71
+    
  72
+    // Get the extension
  73
+    $extension = $matches[count($matches)-1];
  74
+    $extension_test = substr($sammy->uri, -(strlen($extension)+1), (strlen($extension)+1));
  75
+    
  76
+    if( $extension_test == '.' . $extension )
  77
+      $sammy->format = $extension;
73 78
 		
74 79
 		static::$route_found = true;
75 80
 		echo $callback($sammy);
@@ -83,7 +88,12 @@ public function __construct() {
83 88
 	}
84 89
 
85 90
 	public function segment($num) {
86  
-		return isset($this->segments[$num - 1]) ? $this->segments[$num - 1] : null;
  91
+	  $num--;
  92
+	  
  93
+    // Remove the extension
  94
+    $this->segments[$num] = isset($this->segments[$num]) ? rtrim($this->segments[$num], '.' . $this->format) : null;
  95
+	  
  96
+		return isset($this->segments[$num]) ? $this->segments[$num] : null;
87 97
 	}
88 98
 
89 99
 	protected function get_method() {

0 notes on commit 2a5514b

Please sign in to comment.
Something went wrong with that request. Please try again.