Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d0400e

Browse files
committed
allow namespace argument on route groups.
1 parent 768e396 commit 3d0400e

2 files changed

Lines changed: 79 additions & 4 deletions

File tree

src/Illuminate/Routing/Router.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,30 @@ public function mergeWithLastGroup($new)
583583
*/
584584
public static function mergeGroup($new, $old)
585585
{
586+
$new['namespace'] = static::formatUsesPrefix($new, $old);
587+
586588
$new['prefix'] = static::formatGroupPrefix($new, $old);
587589

588-
return array_merge_recursive(array_except($old, array('prefix', 'domain')), $new);
590+
return array_merge_recursive(array_except($old, array('namespace', 'prefix', 'domain')), $new);
591+
}
592+
593+
/**
594+
* Format the uses prefix for the new group attributes.
595+
*
596+
* @param array $new
597+
* @param array $old
598+
* @return string
599+
*/
600+
protected static function formatUsesPrefix($new, $old)
601+
{
602+
if (isset($new['namespace']))
603+
{
604+
return trim(array_get($old, 'namespace'), '\\').'\\'.trim($new['namespace'], '\\');
605+
}
606+
else
607+
{
608+
return array_get($old, 'namespace');
609+
}
589610
}
590611

591612
/**
@@ -716,9 +737,22 @@ protected function getControllerAction($action)
716737
{
717738
if (is_string($action)) $action = array('uses' => $action);
718739

740+
// Here we'll get an instance of this controller dispatcher and hand it off to
741+
// the Closure so it will be used to resolve the class instances out of our
742+
// IoC container instance and call the appropriate methods on the class.
743+
if (count($this->groupStack) > 0)
744+
{
745+
$action['uses'] = $this->prependGroupUses($action['uses']);
746+
}
747+
748+
// Here we'll get an instance of this controller dispatcher and hand it off to
749+
// the Closure so it will be used to resolve the class instances out of our
750+
// IoC container instance and call the appropriate methods on the class.
719751
$action['controller'] = $action['uses'];
720752

721-
return array_set($action, 'uses', $this->getClassClosure($action['uses']));
753+
$closure = $this->getClassClosure($action['uses']);
754+
755+
return array_set($action, 'uses', $closure);
722756
}
723757

724758
/**
@@ -751,6 +785,19 @@ protected function getClassClosure($controller)
751785
};
752786
}
753787

788+
/**
789+
* Prepend the last group uses onto the use clause.
790+
*
791+
* @param string $uses
792+
* @return string
793+
*/
794+
protected function prependGroupUses($uses)
795+
{
796+
$group = last($this->groupStack);
797+
798+
return isset($group['namespace']) ? $group['namespace'].'\\'.$uses : $uses;
799+
}
800+
754801
/**
755802
* Dispatch the request to the application.
756803
*

tests/Routing/RoutingRouteTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ public function testRouteCompilationAgainstUris()
347347
public function testGroupMerging()
348348
{
349349
$old = array('prefix' => 'foo/bar/');
350-
$this->assertEquals(array('prefix' => 'foo/bar/baz'), Router::mergeGroup(array('prefix' => 'baz'), $old));
350+
$this->assertEquals(array('prefix' => 'foo/bar/baz', 'namespace' => null), Router::mergeGroup(array('prefix' => 'baz'), $old));
351351

352352
$old = array('domain' => 'foo');
353-
$this->assertEquals(array('domain' => 'baz', 'prefix' => null), Router::mergeGroup(array('domain' => 'baz'), $old));
353+
$this->assertEquals(array('domain' => 'baz', 'prefix' => null, 'namespace' => null), Router::mergeGroup(array('domain' => 'baz'), $old));
354354
}
355355

356356

@@ -396,6 +396,34 @@ public function testRouteGrouping()
396396
}
397397

398398

399+
public function testMergingControllerUses()
400+
{
401+
$router = $this->getRouter();
402+
$router->group(array('namespace' => 'Namespace'), function() use ($router)
403+
{
404+
$router->get('foo/bar', 'Controller');
405+
});
406+
$routes = $router->getRoutes()->getRoutes();
407+
$action = $routes[0]->getAction();
408+
409+
$this->assertEquals('Namespace\\Controller', $action['controller']);
410+
411+
412+
$router = $this->getRouter();
413+
$router->group(array('namespace' => 'Namespace'), function() use ($router)
414+
{
415+
$router->group(array('namespace' => 'Nested'), function() use ($router)
416+
{
417+
$router->get('foo/bar', 'Controller');
418+
});
419+
});
420+
$routes = $router->getRoutes()->getRoutes();
421+
$action = $routes[0]->getAction();
422+
423+
$this->assertEquals('Namespace\\Nested\\Controller', $action['controller']);
424+
}
425+
426+
399427
public function testResourceRouting()
400428
{
401429
$router = $this->getRouter();

0 commit comments

Comments
 (0)