Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Allow route groups to be loaded from a file directly #16707

Merged
merged 1 commit into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
});