Skip to content

Commit

Permalink
Fixed a bug with my original extension checker for formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
BaylorRae committed Feb 19, 2011
1 parent c39dd34 commit 2a5514b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 8 additions & 2 deletions index.php
Expand Up @@ -17,7 +17,7 @@
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script> <script>


$('#results').load('ajax'); $('#results').load('/ajax');


</script> </script>


Expand Down Expand Up @@ -47,7 +47,13 @@
}); });


get('/hello/(.*?)', function($sammy) { 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() { ajax('/ajax', function() {
Expand Down
16 changes: 13 additions & 3 deletions sammy.php
Expand Up @@ -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) ) { if( static::$route_found || (!preg_match('@^'.$route.'(?:\.(\w+))?$@uD', $sammy->uri, $matches) || $sammy->method != $type) ) {
return false; 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; static::$route_found = true;
echo $callback($sammy); echo $callback($sammy);
Expand All @@ -83,7 +88,12 @@ public function __construct() {
} }


public function segment($num) { 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() { protected function get_method() {
Expand Down

0 comments on commit 2a5514b

Please sign in to comment.