Skip to content

Commit

Permalink
Merge pull request #6186 from kenjis/fix-routes-group-slash
Browse files Browse the repository at this point in the history
fix: `$routes->group('/', ...)` creates the route `foo///bar`
  • Loading branch information
kenjis authored Jun 26, 2022
2 parents 74f12ec + 66fbd47 commit 053d669
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@ public function group(string $name, ...$params)
$oldOptions = $this->currentOptions;

// To register a route, we'll set a flag so that our router
// so it will see the group name.
// will see the group name.
// If the group name is empty, we go on using the previously built group name.
$this->group = $name ? ltrim($oldGroup . '/' . $name, '/') : $oldGroup;
$this->group = $name ? trim($oldGroup . '/' . $name, '/') : $oldGroup;

$callback = array_pop($params);

Expand Down
48 changes: 48 additions & 0 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,54 @@ static function ($routes) {
$this->assertSame($expected, $routes->getRoutes());
}

/**
* @dataProvider groupProvider
*/
public function testNestedGroupingWorksWithRootPrefix(
string $group,
string $subgroup,
array $expected
) {
$routes = $this->getCollector();

$routes->group($group, static function ($routes) use ($subgroup) {
$routes->group(
$subgroup,
static function ($routes) {
$routes->add('users/list', '\Users::list');

$routes->group('delegate', static function ($routes) {
$routes->add('foo', '\Users::foo');
});
}
);
});

$this->assertSame($expected, $routes->getRoutes());
}

public function groupProvider()
{
yield from [
['admin', '/', [
'admin/users/list' => '\Users::list',
'admin/delegate/foo' => '\Users::foo',
]],
['/', '', [
'users/list' => '\Users::list',
'delegate/foo' => '\Users::foo',
]],
['', '', [
'users/list' => '\Users::list',
'delegate/foo' => '\Users::foo',
]],
['', '/', [
'users/list' => '\Users::list',
'delegate/foo' => '\Users::foo',
]],
];
}

public function testHostnameOption()
{
$_SERVER['HTTP_HOST'] = 'example.com';
Expand Down

0 comments on commit 053d669

Please sign in to comment.