From 64d384ddffc5bc23ca641d313214974b8b0a5788 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 7 Sep 2014 22:26:02 -0700 Subject: [PATCH] Splat should consider trailing slash in URLs. --- VERSION | 2 +- flight/net/Route.php | 16 ++++++++-------- tests/RouterTest.php | 12 ++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/VERSION b/VERSION index 0495c4a..e8ea05d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.3 +1.2.4 diff --git a/flight/net/Route.php b/flight/net/Route.php index af04a01..11413c2 100644 --- a/flight/net/Route.php +++ b/flight/net/Route.php @@ -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 diff --git a/tests/RouterTest.php b/tests/RouterTest.php index a57cbe9..3ff7b7b 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -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){