Skip to content

Commit

Permalink
Merge pull request #16707 from JosephSilber/route-group-path
Browse files Browse the repository at this point in the history
[5.4] Allow route groups to be loaded from a file directly
  • Loading branch information
taylorotwell authored Dec 8, 2016
2 parents f23ac64 + 35c445c commit b7f6112
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/Illuminate/Contracts/Routing/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Illuminate\Contracts\Routing;

use Closure;

interface Registrar
{
/**
Expand Down Expand Up @@ -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.
Expand Down
18 changes: 12 additions & 6 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions tests/Routing/fixtures/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$router->get('users', function () {
return 'all-users';
});

0 comments on commit b7f6112

Please sign in to comment.