From 848a73c84e7c131ede12b47f75156e7dc80770cf Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Thu, 9 Mar 2023 17:40:45 +0100 Subject: [PATCH 1/2] Allow getting Route controller class safely, regardless of route type --- src/Illuminate/Routing/Route.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Routing/Route.php b/src/Illuminate/Routing/Route.php index 27cf0a844158..75f958fbc24f 100755 --- a/src/Illuminate/Routing/Route.php +++ b/src/Illuminate/Routing/Route.php @@ -280,11 +280,11 @@ public function getController() /** * Get the controller class used for the route. * - * @return string + * @return string|null */ public function getControllerClass() { - return $this->parseControllerCallback()[0]; + return $this->isControllerAction() ? $this->parseControllerCallback()[0] : null; } /** From c4db8a904c3c3ae171b0cb79244975f5661a0312 Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Thu, 9 Mar 2023 17:47:38 +0100 Subject: [PATCH 2/2] Add test for getting controller class on Route --- tests/Routing/RoutingRouteTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Routing/RoutingRouteTest.php b/tests/Routing/RoutingRouteTest.php index 128dd49b4673..5a1a675d5cf5 100644 --- a/tests/Routing/RoutingRouteTest.php +++ b/tests/Routing/RoutingRouteTest.php @@ -405,6 +405,19 @@ public function testRouteGetAction() $this->assertNull($route->getAction('unknown_property')); } + public function testRouteGetControllerClass() + { + $router = $this->getRouter(); + + $controllerRoute = $router->get('foo/bar')->uses(RouteTestControllerStub::class.'@index'); + $closureRoute = $router->get('foo', function () { + return 'foo'; + }); + + $this->assertSame(RouteTestControllerStub::class, $controllerRoute->getControllerClass()); + $this->assertNull($closureRoute->getControllerClass()); + } + public function testResolvingBindingParameters() { $router = $this->getRouter();