Skip to content

Commit c76c3bd

Browse files
authored
[9.x] Allow route group method to be chained (#44825)
* allow route group method to be chained * update docblocks
1 parent 01a1474 commit c76c3bd

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/Illuminate/Routing/RouteRegistrar.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,13 @@ public function apiResource($name, $controller, array $options = [])
159159
* Create a route group with shared attributes.
160160
*
161161
* @param \Closure|string $callback
162-
* @return void
162+
* @return $this
163163
*/
164164
public function group($callback)
165165
{
166166
$this->router->group($this->attributes, $callback);
167+
168+
return $this;
167169
}
168170

169171
/**

src/Illuminate/Routing/Router.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public function apiResource($name, $controller, array $options = [])
373373
*
374374
* @param array $attributes
375375
* @param \Closure|array|string $routes
376-
* @return void
376+
* @return $this
377377
*/
378378
public function group(array $attributes, $routes)
379379
{
@@ -387,6 +387,8 @@ public function group(array $attributes, $routes)
387387

388388
array_pop($this->groupStack);
389389
}
390+
391+
return $this;
390392
}
391393

392394
/**

tests/Routing/RouteRegistrarTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,26 @@ public function testRouteGroupingWithoutPrefix()
430430
$this->seeResponse('hello', Request::create('bar/baz', 'GET'));
431431
}
432432

433+
public function testRouteGroupChaining()
434+
{
435+
$this->router
436+
->group([], function ($router) {
437+
$router->get('foo', function () {
438+
return 'hello';
439+
});
440+
})
441+
->group([], function ($router) {
442+
$router->get('bar', function () {
443+
return 'goodbye';
444+
});
445+
});
446+
447+
$routeCollection = $this->router->getRoutes();
448+
449+
$this->assertInstanceOf(\Illuminate\Routing\Route::class, $routeCollection->match(Request::create('foo', 'GET')));
450+
$this->assertInstanceOf(\Illuminate\Routing\Route::class, $routeCollection->match(Request::create('bar', 'GET')));
451+
}
452+
433453
public function testRegisteringNonApprovedAttributesThrows()
434454
{
435455
$this->expectException(BadMethodCallException::class);

0 commit comments

Comments
 (0)