diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index e7012e047a63..853b6026af36 100755 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -486,6 +486,12 @@ protected function addRoute($methods, $uri, $action) */ protected function createRoute($methods, $uri, $action) { + // If the action is referenced as an array we will + // convert the array to a regular action string. + if ($this->actionIsReferencedAsArray($action)) { + $action = $this->convertToActionString($action); + } + // If the route is routing to a controller we will parse the route action into // an acceptable array format before registering it and creating this route // instance itself. We need to build the Closure that will call this out. @@ -561,6 +567,36 @@ protected function mergeGroupAttributesIntoRoute($route) $route->setAction($action); } + /** + * Determine if action is referenced as an array. + * + * @param string|array $action + * @return bool + */ + private function actionIsReferencedAsArray($action) + { + if (! is_array($action)) { + return false; + } + + $class = key($action); + + return class_exists($class); + } + + /** + * Convert action array to action string. + * + * @param array $action + * @return string + */ + private function convertToActionString($action) + { + list($class, $method) = each($action); + + return '\\'.$class.'@'.$method; + } + /** * Determine if the action is routing to a controller. * diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index 242f04f64879..2b4e032c85f8 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -568,15 +568,20 @@ protected function getRouteScheme($route) /** * Get the URL to a controller action. * - * @param string $action - * @param mixed $parameters - * @param bool $absolute + * @param array|string $action + * @param mixed $parameters + * @param bool $absolute * @return string * * @throws \InvalidArgumentException */ public function action($action, $parameters = [], $absolute = true) { + if (is_array($action)) { + list($class, $method) = each($action); + $action = '\\'.$class.'@'.$method; + } + if ($this->rootNamespace && ! (strpos($action, '\\') === 0)) { $action = $this->rootNamespace.'\\'.$action; } else { diff --git a/tests/Routing/RoutingRedirectorTest.php b/tests/Routing/RoutingRedirectorTest.php index d084b5be9cd5..55714a3f2879 100644 --- a/tests/Routing/RoutingRedirectorTest.php +++ b/tests/Routing/RoutingRedirectorTest.php @@ -125,6 +125,13 @@ public function testAction() $this->assertEquals('http://foo.com/bar', $response->getTargetUrl()); } + public function testActionAsArray() + { + $this->url->shouldReceive('action')->with([bar::class => 'index'], [])->andReturn('http://foo.com/bar'); + $response = $this->redirect->action([bar::class => 'index']); + $this->assertEquals('http://foo.com/bar', $response->getTargetUrl()); + } + public function testRoute() { $this->url->shouldReceive('route')->with('home')->andReturn('http://foo.com/bar'); diff --git a/tests/Routing/RoutingRouteTest.php b/tests/Routing/RoutingRouteTest.php index 8ea8e43e3a7f..c5248053263e 100644 --- a/tests/Routing/RoutingRouteTest.php +++ b/tests/Routing/RoutingRouteTest.php @@ -928,6 +928,13 @@ public function testControllerInspection() $this->assertEquals('hello', $router->dispatch(Request::create('home/foo', 'GET'))->getContent()); } + public function testRouterActionReferencedAsArray() + { + $router = $this->getRouter(); + $router->get('home/foo', [RouteTestInspectedControllerStub::class => 'getFoo']); + $this->assertEquals('hello', $router->dispatch(Request::create('home/foo', 'GET'))->getContent()); + } + protected function getRouter() { return new Router(new Illuminate\Events\Dispatcher);