From e72248c744bf8824eada9fd07860ebdebe820760 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Mon, 22 Dec 2014 12:10:34 +0100 Subject: [PATCH 1/4] add failing test for #98 --- tests/AltoRouterTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/AltoRouterTest.php b/tests/AltoRouterTest.php index 2a9160c..22ca703 100644 --- a/tests/AltoRouterTest.php +++ b/tests/AltoRouterTest.php @@ -364,6 +364,37 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase 'name' => 'bar_route' ), $this->router->match('/bar/test/do.json', 'GET')); + $this->assertEquals(array( + 'target' => 'bar_action', + 'params' => array( + 'controller' => 'test', + 'action' => 'do' + ), + 'name' => 'bar_route' + ), $this->router->match('/bar/test/do', 'GET')); + } + + /** + * GitHub #98 + */ + public function testMatchWithOptionalPartOnBareUrl(){ + $this->router->map('GET', '/[i:page]?', 'bare_action', 'bare_route'); + + $this->assertEquals(array( + 'target' => 'bare_action', + 'params' => array( + 'page' => 1 + ), + 'name' => 'bare_route' + ), $this->router->match('/1', 'GET')); + + $this->assertEquals(array( + 'target' => 'bare_action', + 'params' => array( + + ), + 'name' => 'bare_route' + ), $this->router->match('/', 'GET')); } public function testMatchWithWildcard() From f18a57d481d91df93ac497acd6d4cacf73603c9d Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Mon, 22 Dec 2014 12:18:10 +0100 Subject: [PATCH 2/4] add failing test for generate --- tests/AltoRouterTest.php | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/tests/AltoRouterTest.php b/tests/AltoRouterTest.php index 22ca703..9444549 100644 --- a/tests/AltoRouterTest.php +++ b/tests/AltoRouterTest.php @@ -233,7 +233,27 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase $this->assertEquals('/test/someaction.json', $this->router->generate('bar_route', $params)); } - + + /** + * GitHub #98 + */ + public function testGenerateWithOptionalPartOnBareUrl() + { + $this->router->map('GET', '/[i:page]?', function(){}, 'bare_route'); + + $params = array( + 'page' => 1 + ); + + $this->assertEquals('/1', + $this->router->generate('bare_route', $params)); + + $params = array(); + + $this->assertEquals('/', + $this->router->generate('bare_route', $params)); + } + public function testGenerateWithNonexistingRoute() { try{ @@ -390,9 +410,7 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase $this->assertEquals(array( 'target' => 'bare_action', - 'params' => array( - - ), + 'params' => array(), 'name' => 'bare_route' ), $this->router->match('/', 'GET')); } From be64536dcbc3632f663ae572570d8b4f892eeed6 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Mon, 22 Dec 2014 13:05:02 +0100 Subject: [PATCH 3/4] prevent strip of preceding slash at base --- AltoRouter.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/AltoRouter.php b/AltoRouter.php index 04f9bba..fe0aaac 100644 --- a/AltoRouter.php +++ b/AltoRouter.php @@ -139,7 +139,7 @@ class AltoRouter { if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { - foreach($matches as $match) { + foreach($matches as $index => $match) { list($block, $pre, $type, $param, $optional) = $match; if ($pre) { @@ -147,13 +147,17 @@ class AltoRouter { } if(isset($params[$param])) { + // Part is found, replace for param value $url = str_replace($block, $params[$param], $url); - } elseif ($optional) { + } elseif ($optional && $index !== 0) { + // Only strip preceeding slash if it's not at the base $url = str_replace($pre . $block, '', $url); + } else { + // Strip match block + $url = str_replace($block, '', $url); } } - } return $url; From 72dd5199bd590a194b877a5104d44ed5717af3ed Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 2 Apr 2017 20:34:42 +0200 Subject: [PATCH 4/4] multi optional --- AltoRouter.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/AltoRouter.php b/AltoRouter.php index fe0aaac..8b3f631 100644 --- a/AltoRouter.php +++ b/AltoRouter.php @@ -254,14 +254,18 @@ class AltoRouter { $pre = '\.'; } + $optional = $optional !== '' ? '?' : null; + //Older versions of PCRE require the 'P' in (?P) $pattern = '(?:' . ($pre !== '' ? $pre : null) . '(' . ($param !== '' ? "?P<$param>" : null) . $type - . '))' - . ($optional !== '' ? '?' : null); + . ')' + . $optional + . ')' + . $optional; $route = str_replace($block, $pattern, $route); }