diff --git a/README.md b/README.md index 5a991a0..f3946c9 100644 --- a/README.md +++ b/README.md @@ -148,11 +148,14 @@ $router->get(['/user/{name}', 'username'], function($name){ // Use the routename and pass in any route parameters to reverse engineer an existing route path // If you change your route path above, you won't need to go through your code updating any links/references to that route -$router->route('username', 'joe'); -// string(9) '/user/joe' +$router->route('username', ['joe']); +// string(8) 'user/joe' $router->route('page', ['intro', 456]); -// string(15) '/page/intro/456' +// string(14) 'page/intro/456' + +$router->route('product', [1], true); +// string(11) '/product/1/' ``` diff --git a/src/Phroute/RouteCollector.php b/src/Phroute/RouteCollector.php index 25aba08..7178e2f 100644 --- a/src/Phroute/RouteCollector.php +++ b/src/Phroute/RouteCollector.php @@ -70,9 +70,10 @@ public function hasRoute($name) { /** * @param $name * @param array $args + * @param bool $wrapWithSlashes * @return string */ - public function route($name, array $args = null) + public function route($name, array $args = null, $wrapWithSlashes = false) { $url = []; @@ -101,7 +102,9 @@ public function route($name, array $args = null) } } - return implode('', $url); + $wrapChar = $wrapWithSlashes ? '/' : ''; + + return $wrapChar . implode('', $url) . $wrapChar; } /** diff --git a/test/Dispatcher/DispatcherTest.php b/test/Dispatcher/DispatcherTest.php index 720c2c1..6c2c6f0 100644 --- a/test/Dispatcher/DispatcherTest.php +++ b/test/Dispatcher/DispatcherTest.php @@ -178,6 +178,19 @@ public function testOptionalReverseRoute() $this->assertEquals('products/store/1', $r->route('products', array(1))); } + public function testReverseRouteWrappedWithSlashes() + { + $r = $this->router(); + + $r->any( ['products/store', 'products'], [__NAMESPACE__.'\\Test','route']); + + $this->assertEquals('/products/store/', $r->route('products', null, true)); + + $r->any( ['products/store/{store:i}', 'products'], [__NAMESPACE__.'\\Test','route']); + + $this->assertEquals('/products/store/1/', $r->route('products', [1], true)); + } + public function testReverseRouteWithDashes() { $r = $this->router();