Skip to content

Fix this error

Laravel: Target class [Controller] does not exist

Dasun Kalupahana 194 views

The error

Error

Target class [UserController] does not exist.

Why it happens

Since Laravel 8, routes are not prefixed with the App\Http\Controllers namespace any more. A route written as 'UserController@index' (a string) can't be found. A typo in the class or namespace, or a missing use line, gives the same error.

How to fix it

  1. Import the controller at the top of routes/web.php and use the array syntax.
  2. Check that the file name, class name and namespace match exactly (Linux is case-sensitive).
  3. Run composer dump-autoload if you moved or renamed the class.
use App\Http\Controllers\UserController;

Route::get('/users', [UserController::class, 'index']);
Still stuck? Send us the full error and what you tried. We add the answers here.

Learn more on the blog

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.