Permalink
Browse files

allow namespace argument on route groups.

  • Loading branch information...
taylorotwell committed Sep 30, 2013
1 parent 768e396 commit 3d0400e55a81b79d3352670f2e24f7358eb95d10
Showing with 79 additions and 4 deletions.
  1. +49 −2 src/Illuminate/Routing/Router.php
  2. +30 −2 tests/Routing/RoutingRouteTest.php
@@ -583,9 +583,30 @@ public function mergeWithLastGroup($new)
*/
public static function mergeGroup($new, $old)
{
$new['namespace'] = static::formatUsesPrefix($new, $old);
$new['prefix'] = static::formatGroupPrefix($new, $old);
return array_merge_recursive(array_except($old, array('prefix', 'domain')), $new);
return array_merge_recursive(array_except($old, array('namespace', 'prefix', 'domain')), $new);
}
/**
* Format the uses prefix for the new group attributes.
*
* @param array $new
* @param array $old
* @return string
*/
protected static function formatUsesPrefix($new, $old)
{
if (isset($new['namespace']))
{
return trim(array_get($old, 'namespace'), '\\').'\\'.trim($new['namespace'], '\\');
}
else
{
return array_get($old, 'namespace');
}
}
/**
@@ -716,9 +737,22 @@ protected function getControllerAction($action)
{
if (is_string($action)) $action = array('uses' => $action);
// Here we'll get an instance of this controller dispatcher and hand it off to
// the Closure so it will be used to resolve the class instances out of our
// IoC container instance and call the appropriate methods on the class.
if (count($this->groupStack) > 0)
{
$action['uses'] = $this->prependGroupUses($action['uses']);
}
// Here we'll get an instance of this controller dispatcher and hand it off to
// the Closure so it will be used to resolve the class instances out of our
// IoC container instance and call the appropriate methods on the class.
$action['controller'] = $action['uses'];
return array_set($action, 'uses', $this->getClassClosure($action['uses']));
$closure = $this->getClassClosure($action['uses']);
return array_set($action, 'uses', $closure);
}
/**
@@ -751,6 +785,19 @@ protected function getClassClosure($controller)
};
}
/**
* Prepend the last group uses onto the use clause.
*
* @param string $uses
* @return string
*/
protected function prependGroupUses($uses)
{
$group = last($this->groupStack);
return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses;
}
/**
* Dispatch the request to the application.
*
@@ -347,10 +347,10 @@ public function testRouteCompilationAgainstUris()
public function testGroupMerging()
{
$old = array('prefix' => 'foo/bar/');
$this->assertEquals(array('prefix' => 'foo/bar/baz'), Router::mergeGroup(array('prefix' => 'baz'), $old));
$this->assertEquals(array('prefix' => 'foo/bar/baz', 'namespace' => null), Router::mergeGroup(array('prefix' => 'baz'), $old));
$old = array('domain' => 'foo');
$this->assertEquals(array('domain' => 'baz', 'prefix' => null), Router::mergeGroup(array('domain' => 'baz'), $old));
$this->assertEquals(array('domain' => 'baz', 'prefix' => null, 'namespace' => null), Router::mergeGroup(array('domain' => 'baz'), $old));
}
@@ -396,6 +396,34 @@ public function testRouteGrouping()
}
public function testMergingControllerUses()
{
$router = $this->getRouter();
$router->group(array('namespace' => 'Namespace'), function() use ($router)
{
$router->get('foo/bar', 'Controller');
});
$routes = $router->getRoutes()->getRoutes();
$action = $routes[0]->getAction();
$this->assertEquals('Namespace\\Controller', $action['controller']);
$router = $this->getRouter();
$router->group(array('namespace' => 'Namespace'), function() use ($router)
{
$router->group(array('namespace' => 'Nested'), function() use ($router)
{
$router->get('foo/bar', 'Controller');
});
});
$routes = $router->getRoutes()->getRoutes();
$action = $routes[0]->getAction();
$this->assertEquals('Namespace\\Nested\\Controller', $action['controller']);
}
public function testResourceRouting()
{
$router = $this->getRouter();

0 comments on commit 3d0400e

Please sign in to comment.