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

@ -177,6 +177,18 @@ class RouterTest extends PHPUnit_Framework_TestCase
$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
function testSplatNamedPlusWildcard(){
$this->router->map('/account/@name/*', function($name, $route){

Loading…
Cancel
Save