From 35c445cfb3f70935c50d3efc2aa69c274cea004e Mon Sep 17 00:00:00 2001 From: Joseph Silber Date: Sun, 31 Jul 2016 09:03:27 -0400 Subject: [PATCH] Allow route groups to be loaded from a file directly --- src/Illuminate/Contracts/Routing/Registrar.php | 8 +++----- src/Illuminate/Routing/Router.php | 18 ++++++++++++------ tests/Routing/RoutingRouteTest.php | 12 ++++++++++++ tests/Routing/fixtures/routes.php | 5 +++++ 4 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 tests/Routing/fixtures/routes.php diff --git a/src/Illuminate/Contracts/Routing/Registrar.php b/src/Illuminate/Contracts/Routing/Registrar.php index 0df149146475..bf1c50a00927 100644 --- a/src/Illuminate/Contracts/Routing/Registrar.php +++ b/src/Illuminate/Contracts/Routing/Registrar.php @@ -2,8 +2,6 @@ namespace Illuminate\Contracts\Routing; -use Closure; - interface Registrar { /** @@ -83,11 +81,11 @@ public function resource($name, $controller, array $options = []); /** * Create a route group with shared attributes. * - * @param array $attributes - * @param \Closure $callback + * @param array $attributes + * @param \Closure|string $routes * @return void */ - public function group(array $attributes, Closure $callback); + public function group(array $attributes, $routes); /** * Substitute the route bindings onto the route. diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index 29b7a804b6d9..cb6b25d66350 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -320,17 +320,23 @@ public function auth() * Create a route group with shared attributes. * * @param array $attributes - * @param \Closure $callback + * @param \Closure|string $routes * @return void */ - public function group(array $attributes, Closure $callback) + public function group(array $attributes, $routes) { $this->updateGroupStack($attributes); - // Once we have updated the group stack, we will execute the user Closure and - // merge in the groups attributes when the route is created. After we have - // run the callback, we will pop the attributes off of this group stack. - call_user_func($callback, $this); + // Once we have updated the group stack, we'll load the provided routes and + // merge in the group's attributes when the routes are created. After we + // have created the routes, we will pop the attributes off the stack. + if ($routes instanceof Closure) { + $routes($this); + } else { + $router = $this; + + require $routes; + } array_pop($this->groupStack); } diff --git a/tests/Routing/RoutingRouteTest.php b/tests/Routing/RoutingRouteTest.php index bc0956b4e9a8..759f57607586 100644 --- a/tests/Routing/RoutingRouteTest.php +++ b/tests/Routing/RoutingRouteTest.php @@ -729,6 +729,18 @@ public function testRouteGrouping() $this->assertEquals('foo', $routes[0]->getPrefix()); } + public function testRouteGroupingFromFile() + { + $router = $this->getRouter(); + $router->group(['prefix' => 'api'], __DIR__.'/fixtures/routes.php'); + + $route = last($router->getRoutes()->get()); + $request = Request::create('api/users', 'GET'); + + $this->assertTrue($route->matches($request)); + $this->assertEquals('all-users', $route->bind($request)->run($request)); + } + public function testRouteGroupingWithAs() { $router = $this->getRouter(); diff --git a/tests/Routing/fixtures/routes.php b/tests/Routing/fixtures/routes.php new file mode 100644 index 000000000000..0701ae808408 --- /dev/null +++ b/tests/Routing/fixtures/routes.php @@ -0,0 +1,5 @@ +get('users', function () { + return 'all-users'; +});