Skip to content

Commit

Permalink
[9.x] Allow route group method to be chained (#44825)
Browse files Browse the repository at this point in the history
* allow route group method to be chained

* update docblocks
  • Loading branch information
ockle authored Nov 4, 2022
1 parent 01a1474 commit c76c3bd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Illuminate/Routing/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ public function apiResource($name, $controller, array $options = [])
* Create a route group with shared attributes.
*
* @param \Closure|string $callback
* @return void
* @return $this
*/
public function group($callback)
{
$this->router->group($this->attributes, $callback);

return $this;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function apiResource($name, $controller, array $options = [])
*
* @param array $attributes
* @param \Closure|array|string $routes
* @return void
* @return $this
*/
public function group(array $attributes, $routes)
{
Expand All @@ -387,6 +387,8 @@ public function group(array $attributes, $routes)

array_pop($this->groupStack);
}

return $this;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,26 @@ public function testRouteGroupingWithoutPrefix()
$this->seeResponse('hello', Request::create('bar/baz', 'GET'));
}

public function testRouteGroupChaining()
{
$this->router
->group([], function ($router) {
$router->get('foo', function () {
return 'hello';
});
})
->group([], function ($router) {
$router->get('bar', function () {
return 'goodbye';
});
});

$routeCollection = $this->router->getRoutes();

$this->assertInstanceOf(\Illuminate\Routing\Route::class, $routeCollection->match(Request::create('foo', 'GET')));
$this->assertInstanceOf(\Illuminate\Routing\Route::class, $routeCollection->match(Request::create('bar', 'GET')));
}

public function testRegisteringNonApprovedAttributesThrows()
{
$this->expectException(BadMethodCallException::class);
Expand Down

0 comments on commit c76c3bd

Please sign in to comment.