Skip to content

Commit 053d669

Browse files
authored
Merge pull request #6186 from kenjis/fix-routes-group-slash
fix: `$routes->group('/', ...)` creates the route `foo///bar`
2 parents 74f12ec + 66fbd47 commit 053d669

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

system/Router/RouteCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,9 @@ public function group(string $name, ...$params)
623623
$oldOptions = $this->currentOptions;
624624

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

630630
$callback = array_pop($params);
631631

tests/system/Router/RouteCollectionTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,54 @@ static function ($routes) {
375375
$this->assertSame($expected, $routes->getRoutes());
376376
}
377377

378+
/**
379+
* @dataProvider groupProvider
380+
*/
381+
public function testNestedGroupingWorksWithRootPrefix(
382+
string $group,
383+
string $subgroup,
384+
array $expected
385+
) {
386+
$routes = $this->getCollector();
387+
388+
$routes->group($group, static function ($routes) use ($subgroup) {
389+
$routes->group(
390+
$subgroup,
391+
static function ($routes) {
392+
$routes->add('users/list', '\Users::list');
393+
394+
$routes->group('delegate', static function ($routes) {
395+
$routes->add('foo', '\Users::foo');
396+
});
397+
}
398+
);
399+
});
400+
401+
$this->assertSame($expected, $routes->getRoutes());
402+
}
403+
404+
public function groupProvider()
405+
{
406+
yield from [
407+
['admin', '/', [
408+
'admin/users/list' => '\Users::list',
409+
'admin/delegate/foo' => '\Users::foo',
410+
]],
411+
['/', '', [
412+
'users/list' => '\Users::list',
413+
'delegate/foo' => '\Users::foo',
414+
]],
415+
['', '', [
416+
'users/list' => '\Users::list',
417+
'delegate/foo' => '\Users::foo',
418+
]],
419+
['', '/', [
420+
'users/list' => '\Users::list',
421+
'delegate/foo' => '\Users::foo',
422+
]],
423+
];
424+
}
425+
378426
public function testHostnameOption()
379427
{
380428
$_SERVER['HTTP_HOST'] = 'example.com';

0 commit comments

Comments
 (0)