Home > Mobile >  Target class does not exist in Laravel
Target class does not exist in Laravel

Time:10-03

this is the route from api.php :

 Route::apiResource('friend-request', FriendRequestController::class)->except('delete');

the controller already exists and i'm calling it on a store using a post request :

FriendRequestController.php

class FriendRequestController extends ApiController
{
    public function store(StoreFriendRequest $request, FriendRequestService $friendRequestService,UserService $userService)
    {
      //code
    }

i get this error after calling this route

message: "Target class [App\Http\Controllers\App\Http\Controllers\FriendRequestController] does not exist."

i guess it's because of the form request what do you think ?

CodePudding user response:

This is very probably because the $namespace property of the app/Providers/RouteServiceProvider.php file is uncommented.

It means that every controllers in your route file will have the default \App\Http\Controllers namespace prefixed.

So \App\Http\Controllers\FriendRequestController becomes \App\Http\Controllers\App\Http\Controllers\FriendRequestController and so on...

Since you are using the "new" route declaration notation (using the fully qualified namespace), you don't need a "default" namespace:

 Route::apiResource('friend-request', FriendRequestController::class)->except('delete');

// FriendRequestController::class === \App\Http\Controllers\FriendRequestController



// However in the old declaration:
 Route::apiResource('friend-request', 'FriendRequestController')->except('delete');

// You were only using the base class name, so the prefix was here to help Laravel find the "entry point" of your controllers
// which is \App\Http\Controllers\

All this is detailled in the migration guide: https://laravel.com/docs/8.x/upgrade#automatic-controller-namespace-prefixing

  • Related