Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,13 @@ protected function getClassClosure($controller)
// Now we can split the controller and method out of the action string so that we
// can call them appropriately on the class. This controller and method are in
// in the Class@method format and we need to explode them out then use them.
[$class, $method] = explode('@', $controller);
$controller = explode('@', $controller);
$class = $controller[0];

return $d->dispatch($route, $request, $class, $method);
// if route action doesn't define method, then by default use `__invoke` method, thus invokable action
$method = $controller[1] ?? '__invoke';

return $d->dispatch($route, $request, $class, $method);
};
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,21 @@ public function testRouteRedirect()
}


public function testDispatchingCallableActionClasses()
{
$router = $this->getRouter();
$router->get('foo/bar', 'ActionStub');

$this->assertEquals('hello', $router->dispatch(Request::create('foo/bar', 'GET'))->getContent());

$router->get('foo/bar2', [
'uses' => 'ActionStub',
]);

$this->assertEquals('hello', $router->dispatch(Request::create('foo/bar2', 'GET'))->getContent());
}


protected function getRouter(): Router
{
return new Router(new Illuminate\Events\Dispatcher);
Expand Down Expand Up @@ -994,3 +1009,11 @@ public function handle(): string
return 'handling!';
}
}

class ActionStub extends Controller
{
public function __invoke(): string
{
return 'hello';
}
}