Summer Sale! All accounts are 50% off this week.

ChrisF79's avatar

Why won't this route work?

I have a route in Laravel 8.11 that just won't work.

Route::get('/', [HomeController::class, 'index']);

There is a HomeController.php and an index method yet the error says, "Target class [HomeController] does not exist." I disagree. What's up?

0 likes
5 replies
ChrisF79's avatar

That was it! I didn't know I had to do that. Thanks!

Max_m's avatar

@Chrisf79 : so could you explain how it worked with you? i am facing the exact same problem. thanx

ChrisF79's avatar

Hey Max_m!

Yes, you need the namespacing at the top of your web.php route file. In my case, my controller was called HomeController so the namespace at the top is: use App\Http\Controllers\HomeController;

The route itself is:

Route::get('/', [HomeController::class, 'index']);

Worked like a charm after that! I hope that helps you!

1 like

Please or to participate in this conversation.