Ad

Laravel Target Class [App\Http\Controllers\App\Http\Controllers\ApiController] Does Not Exist

- 1 answer

For some reason, which is probably my fault, Laravel thinks it should be looking for the class ApiController in path: 'App\Http\Controllers\App\Http\Controllers', so... it doubles, but I have no idea why.

It's a brand new Laravel 6 project, I've created the ApiController with the make:controller artisan command and added a function, like this:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ApiController extends Controller
{
    public function base() {
        return 'This is a test function';
    }
}

Then I've added a route to the api routes like this:

use App\Http\Controllers\ApiController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::group(['prefix' => '/v1', 'as' => 'api'], function () {
    Route::get('/base', ['uses' => ApiController::class . '@base'])->name('base');
});

As you can see, I've even 'imported' the controller, but it just can't find it. That's it, no other files or changes to the project. Also tried clearing route cache and dump-autoload, but that did not change anything.

Ad

Answer

If you wanna ::class reference in the router, it should be done like this.

Route::group(['prefix' => '/v1', 'as' => 'api'], function () {
    Route::get('base', [ApiController::class, 'base'])->name('base');
});
Ad
source: stackoverflow.com
Ad