0

I am getting the following error in my Laravel application at http://127.0.0.1:8000/user. Please understand, this is my first ever Laravel application.

Illuminate\Contracts\Container\BindingResolutionException Target class
[UserController] does not exist.

When I am trying the following code in web.php Route::get('/user', 'UserController@index');.

UserController file:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
    public function index()
    {
        return 'Hellow dear user!';
    }
}

However, this works.

Route::get('/user', function(){
    return view('user');
});

The migration was ran and the tables are successfully created in the database.

Environment:

OS: XAMPP on Windows 10
Laravel version: 8.5.0
Laravel locale: en
Laravel config cached: false
PHP version: 7.4.3

The full error report is shared at Flare

Subrata Sarkar
  • 2,975
  • 6
  • 45
  • 85
  • 3
    This question has been asked a bunch of times already; https://stackoverflow.com/questions/63807759/laravel-8-route-cannot-find-controllers-target-class-auth-logincontroller-d, https://stackoverflow.com/questions/63807930/target-class-controller-does-not-exist-laravel-8, etc. Laravel 8 uses new syntax for Routes, the above is not valid. – Tim Lewis Sep 21 '20 at 14:26

1 Answers1

4

May be this will work -

use App\Http\Controllers\UserController;

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

//or

Route::get('/user', 'App\Http\Controllers\UserController@index');
Jaynil Savani
  • 310
  • 2
  • 9