Illuminate \ Contracts \ Container \ BindingResolutionException PHP 8.3.3 10.45.1 Target class [PagesController] does not exist.
Hi everyone,
I recently encountered an issue in my Laravel application when defining routes using the Route::get method. I thought I'd share the problem and my solution in case anyone else runs into the same issue.
When defining a route to my controller without specifying the full namespace, like this:
Route::get('/', 'PagesController@home');
I received an error. This seemed to be because Laravel couldn't locate the PagesController correctly.
Illuminate \ Contracts \ Container \ BindingResolutionException PHP 8.3.3 10.45.1 Target class [PagesController] does not exist.
After some troubleshooting, I found that explicitly providing the full namespace to the controller within the route definition resolved the issue. Here's how I modified my routes in routes/web.php:
That's one solution but you can also just add use App\Http\Controllers\PagesController; at the top in your routes file and then Route::get('/', 'PagesController@home'); would work fine.
@gych Thank you for your suggestion! I appreciate your input and decided to give it a try. Unfortunately, I encountered same error . This error seems to indicate that Laravel is still unable to resolve the PagesController class, even with the use statement explicitly importing it. if you have any further insights or suggestions on what might be causing this error, I'd be very grateful to hear them. Thanks again for your help!
@gych That's not right. That's the old syntax and imports won't have an effect there. You have to do:
use App\Http\Controllers\PagesController;
...
Route::get('/', [PagesController::class, 'home']);
Or if you want to use the old @-syntax (I thought it was deprecated?), you have to either use the fully qualified class name or define a default namespace in the route service provider. Though I'm not sure if the default namespace is supported anymore.