1

How to fix ‘Target class does not exist’ in Laravel 8

I have applied all three of these fixes but I am still getting an error:

  • Add the namespace back manually so you can use it as you did in Laravel 7.x and before
  • Use the full namespace in your route files when using the string syntax
  • Use the action syntax (recommended)
use App\Http\Controllers\EventController;
use App\Http\Controllers\FamilyController;
use App\Http\Controllers\HedgehogController;
use App\Http\Controllers\KeywordController;
use App\Http\Controllers\CommentController;
Route::get('/comments', [App\Http\Controllers\CommentController::class, 'index']);

Is there anything else I can do to fix this?

miken32
  • 42,008
  • 16
  • 111
  • 154
Dan'l
  • 99
  • 1
  • 1
  • 6
  • 1
    please provide the exact error message – lagbox Jan 05 '22 at 17:13
  • Target class [App\Http\MemorysController] does not exist – Dan'l Jan 05 '22 at 19:12
  • well that class probably doesn't exist, since the Controllers should be in the `App\Http\Controllers` namespace (but depends upon where you actually placed this file) ... also you are not showing anything here related to that class (like a route definition) – lagbox Jan 05 '22 at 19:13
  • 2
    Does this answer your question? [Target class controller does not exist - Laravel 8](https://stackoverflow.com/questions/63807930/target-class-controller-does-not-exist-laravel-8) – miken32 Jan 05 '22 at 21:02
  • are you going to follow up on your own question? – lagbox Jan 06 '22 at 13:56
  • No, it did not help. It suggested using Fully Qualified Path (FQP). I have used the FQP but still get DNE error. My controller's use section: namespace App\Http\Controllers; use App\Models\Event; use App\Models\Memorys; : class MemorysController extends Controller . The routes: //memorys Route::get('/memorys', [MemorysController::class, 'index']); Route::get('/memorys/create/{id}', [MemorysController::class,'create']); Route::post('/memorys/', [MemorysController::class, 'store']); – Dan'l Jan 06 '22 at 17:40

5 Answers5

2

Please open your controller again then fix their namespace

then php artisan config:cache do php artisan config:clear before cache if necessary

then reimport the Class then

php artisan route:cache
1

I ran into this same problem and i ran

php artisan optimize:clear

this fixed it for me

-1

You have already import the namespace so use directly the controller like this

use App\Http\Controllers\CommentController;
Route::get('/comments', [CommentController::class, 'index']);

https://laravel.com/docs/8.x/routing#the-default-route-files

KRA
  • 22
  • 1
  • 7
-1

Uncomment protected $namespace = 'App\\Http\\Controllers'; in the RouteServiceProvider.

Found here:

Defining a namespace for Laravel 8 routes

ouflak
  • 2,458
  • 10
  • 44
  • 49
Dan'l
  • 99
  • 1
  • 1
  • 6
-1

It's a cache problem. You have to cache the routes. DONT TOUCH YOUR CODES

php artisan config:clear  <=====
php artisan route:cache  <=====
General Grievance
  • 4,555
  • 31
  • 31
  • 45