From c76c3bdddf3bac41f92bbcaddb50d1053d0011f2 Mon Sep 17 00:00:00 2001 From: ockle Date: Fri, 4 Nov 2022 21:12:41 +0000 Subject: [PATCH] [9.x] Allow route group method to be chained (#44825) * allow route group method to be chained * update docblocks --- src/Illuminate/Routing/RouteRegistrar.php | 4 +++- src/Illuminate/Routing/Router.php | 4 +++- tests/Routing/RouteRegistrarTest.php | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Routing/RouteRegistrar.php b/src/Illuminate/Routing/RouteRegistrar.php index aa22f3deb652..520a6fa967d8 100644 --- a/src/Illuminate/Routing/RouteRegistrar.php +++ b/src/Illuminate/Routing/RouteRegistrar.php @@ -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; } /** diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index d5ab01928351..88d47f89f660 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -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) { @@ -387,6 +387,8 @@ public function group(array $attributes, $routes) array_pop($this->groupStack); } + + return $this; } /** diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 5449bcaf5fc4..aba801b36873 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -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);