Laravel: Not picking up __invoke method?


Trying to use invokable controllers, but it seems to fail to find the __invoke method?

Invalid route action: [App\Http\Controllers\App\Http\Controllers\MainController].

It seems to be returning true on:

if (! method_exists($action, '__invoke')) {
    throw new UnexpectedValueException("Invalid route action: [{$action}].");



Route::get('/', \App\Http\Controllers\MainController::class);



namespace App\Http\Controllers;

class MainController extends Controller
    public function __invoke()


Laravel by default assumes that your controllers will be located at App\Http\Controllers\. So when you’re adding the full path to your controller, Laravel will check it there, at App\Http\Controllers\App\Http\Controllers\MainController.

To solve it simply remove the namespace when you’re registering the route, and register it like this:

Route::get('/', MainController::class);

Alternatively, you can stop this behavior by removing ->namespace($this->namespace) from mapWebRoutes() method on RouteServiceProvider class, which is located at App\Providers folder. Then you can register your routes like this:

Route::get('/', \App\Http\Controllers\MainController::class);

Answered By – Ahmad H.

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published