Skip to content

Commit

Permalink
Merge pull request #539 from flightphp/route-group-fix
Browse files Browse the repository at this point in the history
fixed bug with grouped routes
  • Loading branch information
fadrian06 authored Feb 7, 2024
2 parents 6e40f79 + 72882b2 commit 8bec365
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
28 changes: 26 additions & 2 deletions flight/net/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Router
*/
protected array $group_middlewares = [];

/**
* Allowed HTTP methods
*
* @var array<int, string>
*/
protected array $allowed_methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];

/**
* Gets mapped routes.
*
Expand Down Expand Up @@ -74,7 +81,19 @@ public function clear(): void
*/
public function map(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): Route
{
$url = trim($pattern);

// This means that the route ies defined in a group, but the defined route is the base
// url path. Note the '' in route()
// Ex: Flight::group('/api', function() {
// Flight::route('', function() {});
// }
// Keep the space so that it can execute the below code normally
if ($this->group_prefix !== '') {
$url = ltrim($pattern);
} else {
$url = trim($pattern);
}

$methods = ['*'];

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

$route = new Route($this->group_prefix . $url, $callback, $methods, $pass_route, $route_alias);
// And this finishes it off.
if ($this->group_prefix !== '') {
$url = rtrim($this->group_prefix . $url);
}

$route = new Route($url, $callback, $methods, $pass_route, $route_alias);

// to handle group middleware
foreach ($this->group_middlewares as $gm) {
Expand Down
35 changes: 35 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,41 @@ public function testGroupRoutes()
$this->check('123');
}

public function testGroupRouteWithEmptyMapPath()
{
$this->router->group('/user', function (Router $router) {
$router->map('', function () {
echo "I'm a little teapot";
});
});
$this->request->url = '/user';
$this->check('I\'m a little teapot');
}

public function testGroupRouteWithEmptyGetPath()
{
$this->router->group('/user', function (Router $router) {
$router->get('', function () {
echo "I'm a little teapot";
});
});
$this->request->url = '/user';
$this->request->method = 'GET';
$this->check('I\'m a little teapot');
}

public function testGroupRouteWithEmptyMultipleMethodsPath()
{
$this->router->group('/user', function (Router $router) {
$router->map('GET|POST ', function () {
echo "I'm a little teapot";
});
});
$this->request->url = '/user';
$this->request->method = 'GET';
$this->check('I\'m a little teapot');
}

public function testGroupRoutesMultiParams()
{
$this->router->group('/user', function (Router $router) {
Expand Down

0 comments on commit 8bec365

Please sign in to comment.