Skip to content

Commit

Permalink
allow namespace argument on route groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 30, 2013
1 parent 768e396 commit 3d0400e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
51 changes: 49 additions & 2 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
32 changes: 30 additions & 2 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3d0400e

Please sign in to comment.