Splat should consider trailing slash in URLs.

pull/145/head
Mike Cao 11 years ago
parent 88f4c1fb71
commit 64d384ddff

@ -1 +1 @@
1.2.3 1.2.4

@ -42,7 +42,7 @@ class Route {
/** /**
* @var string URL splat content * @var string URL splat content
*/ */
public $splat; public $splat = '';
/** /**
* @var boolean Pass self in callback parameters * @var boolean Pass self in callback parameters
@ -80,10 +80,10 @@ class Route {
} }
$ids = array(); $ids = array();
$char = substr($this->pattern, -1); $last_char = substr($this->pattern, -1);
// Get splat // Get splat
if ($char === '*') { if ($last_char === '*') {
$n = 0; $n = 0;
$len = strlen($url); $len = strlen($url);
$count = substr_count($this->pattern, '/'); $count = substr_count($this->pattern, '/');
@ -93,12 +93,12 @@ class Route {
if ($n == $count) break; if ($n == $count) break;
} }
$this->splat = substr($url, $i+1); $this->splat = (string)substr($url, $i+1);
} }
$this->pattern = str_replace(array(')','*'), array(')?','.*?'), $this->pattern);
// Build the regex for matching // Build the regex for matching
$regex = str_replace(array(')','/*'), array(')?','(/?|/.*?)'), $this->pattern);
$regex = preg_replace_callback( $regex = preg_replace_callback(
'#@([\w]+)(:([^/\(\)]*))?#', '#@([\w]+)(:([^/\(\)]*))?#',
function($matches) use (&$ids) { function($matches) use (&$ids) {
@ -108,11 +108,11 @@ class Route {
} }
return '(?P<'.$matches[1].'>[^/\?]+)'; return '(?P<'.$matches[1].'>[^/\?]+)';
}, },
$this->pattern $regex
); );
// Fix trailing slash // Fix trailing slash
if ($char === '/') { if ($last_char === '/') {
$regex .= '?'; $regex .= '?';
} }
// Allow trailing slash // Allow trailing slash

@ -177,6 +177,18 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->check('456/def/xyz'); $this->check('456/def/xyz');
} }
// Test splat without trailing slash
function testSplatWildcardTrailingSlash(){
$this->router->map('/account/*', function($route){
echo $route->splat;
},
true);
$this->request->url = '/account';
$this->check('');
}
// Test splat with named parameters // Test splat with named parameters
function testSplatNamedPlusWildcard(){ function testSplatNamedPlusWildcard(){
$this->router->map('/account/@name/*', function($name, $route){ $this->router->map('/account/@name/*', function($name, $route){

Loading…
Cancel
Save