Skip to content

Commit

Permalink
Ensure reverse routing provides a leading slash to match the docs and…
Browse files Browse the repository at this point in the history
… ensure a conforming URL. Partial for #278
  • Loading branch information
lonnieezell committed Oct 4, 2016
1 parent ce07f0c commit 1eb08d9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 4 additions & 2 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ public function environment(string $env, \Closure $callback): RouteCollectionInt
*
* @param string $search
* @param ...$params
*
* @return string
*/
public function reverseRoute(string $search, ...$params): string
{
Expand Down Expand Up @@ -964,7 +966,7 @@ protected function fillRouteParams(string $from, array $params = null): string

if (empty($matches[0]))
{
return $from;
return '/'.ltrim($from, '/');
}

// Build our resulting string, inserting the $params in
Expand All @@ -983,7 +985,7 @@ protected function fillRouteParams(string $from, array $params = null): string
}
}

return $from;
return '/'.ltrim($from, '/');
}

//--------------------------------------------------------------------
Expand Down
15 changes: 9 additions & 6 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ public function testEnvironmentRestricts()

//--------------------------------------------------------------------

/**
* @group single
*/
public function testReverseRoutingFindsSimpleMatch()
{
$routes = new RouteCollection();
Expand All @@ -550,7 +553,7 @@ public function testReverseRoutingFindsSimpleMatch()

$match = $routes->reverseRoute('myController::goto', 'string', 13);

$this->assertEquals('path/string/to/13', $match);
$this->assertEquals('/path/string/to/13', $match);
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -590,18 +593,18 @@ public function testReverseRoutingThrowsExceptionWithBadParamTypes()
}

//--------------------------------------------------------------------

public function testNamedRoutes()
{
$routes = new RouteCollection();

$routes->add('users', 'Users::index', ['as' => 'namedRoute']);

$this->assertEquals('users', $routes->reverseRoute('namedRoute'));
$this->assertEquals('/users', $routes->reverseRoute('namedRoute'));
}

//--------------------------------------------------------------------

public function testNamedRoutesFillInParams()
{
$routes = new RouteCollection();
Expand All @@ -610,7 +613,7 @@ public function testNamedRoutesFillInParams()

$match = $routes->reverseRoute('namedRoute', 'string', 13);

$this->assertEquals('path/string/to/13', $match);
$this->assertEquals('/path/string/to/13', $match);
}

//--------------------------------------------------------------------
Expand Down
3 changes: 0 additions & 3 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ public function testAutoRouteFindsControllerWithSubfolder()

//--------------------------------------------------------------------

/**
* @group single
*/
public function testDetectsLocales()
{
$router = new Router($this->collection);
Expand Down

0 comments on commit 1eb08d9

Please sign in to comment.