Skip to content

Commit 8bec365

Browse files
authored
Merge pull request #539 from flightphp/route-group-fix
fixed bug with grouped routes
2 parents 6e40f79 + 72882b2 commit 8bec365

2 files changed

Lines changed: 61 additions & 2 deletions

File tree

flight/net/Router.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class Router
4646
*/
4747
protected array $group_middlewares = [];
4848

49+
/**
50+
* Allowed HTTP methods
51+
*
52+
* @var array<int, string>
53+
*/
54+
protected array $allowed_methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];
55+
4956
/**
5057
* Gets mapped routes.
5158
*
@@ -74,7 +81,19 @@ public function clear(): void
7481
*/
7582
public function map(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): Route
7683
{
77-
$url = trim($pattern);
84+
85+
// This means that the route ies defined in a group, but the defined route is the base
86+
// url path. Note the '' in route()
87+
// Ex: Flight::group('/api', function() {
88+
// Flight::route('', function() {});
89+
// }
90+
// Keep the space so that it can execute the below code normally
91+
if ($this->group_prefix !== '') {
92+
$url = ltrim($pattern);
93+
} else {
94+
$url = trim($pattern);
95+
}
96+
7897
$methods = ['*'];
7998

8099
if (false !== strpos($url, ' ')) {
@@ -83,7 +102,12 @@ public function map(string $pattern, callable $callback, bool $pass_route = fals
83102
$methods = explode('|', $method);
84103
}
85104

86-
$route = new Route($this->group_prefix . $url, $callback, $methods, $pass_route, $route_alias);
105+
// And this finishes it off.
106+
if ($this->group_prefix !== '') {
107+
$url = rtrim($this->group_prefix . $url);
108+
}
109+
110+
$route = new Route($url, $callback, $methods, $pass_route, $route_alias);
87111

88112
// to handle group middleware
89113
foreach ($this->group_middlewares as $gm) {

tests/RouterTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,41 @@ public function testGroupRoutes()
459459
$this->check('123');
460460
}
461461

462+
public function testGroupRouteWithEmptyMapPath()
463+
{
464+
$this->router->group('/user', function (Router $router) {
465+
$router->map('', function () {
466+
echo "I'm a little teapot";
467+
});
468+
});
469+
$this->request->url = '/user';
470+
$this->check('I\'m a little teapot');
471+
}
472+
473+
public function testGroupRouteWithEmptyGetPath()
474+
{
475+
$this->router->group('/user', function (Router $router) {
476+
$router->get('', function () {
477+
echo "I'm a little teapot";
478+
});
479+
});
480+
$this->request->url = '/user';
481+
$this->request->method = 'GET';
482+
$this->check('I\'m a little teapot');
483+
}
484+
485+
public function testGroupRouteWithEmptyMultipleMethodsPath()
486+
{
487+
$this->router->group('/user', function (Router $router) {
488+
$router->map('GET|POST ', function () {
489+
echo "I'm a little teapot";
490+
});
491+
});
492+
$this->request->url = '/user';
493+
$this->request->method = 'GET';
494+
$this->check('I\'m a little teapot');
495+
}
496+
462497
public function testGroupRoutesMultiParams()
463498
{
464499
$this->router->group('/user', function (Router $router) {

0 commit comments

Comments
 (0)