Reputation: 390
I have a Laravel application where I'm getting exception controller does not exist due to wrong namespace. The namespace is getting repeated two times causing this issue. I don't know why it's repeating and I've been searching on internet but nothing found related to this.
Target class [App\Http\Controllers\App\Http\Controllers\customerController] does not exist.
The namespace here is being repeated which is causing the error. The namespace set in RouteServiceProvider is below:
protected $namespace = 'App\\Http\\Controllers';
My routes in routes/web.php file are:
use App\Http\Controllers\customerController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
Route::resource("customers", customerController::class);
If I use old Laravel syntax then the error goes away:
Route:resource("customers", "customerController");
But I want to know why the above approach is behaving strangely. I'm using Laravel 8 in this project.
I was doing one thing wrong. We don't have to specify namespace in RouteServiceProvider
if we want to use classes as routes. I added namespace in routes services provider, that's why when I was using class syntax, it included namespace twice causing error. But when I used string based controller name then namespace resolved correctly.
Upvotes: 1
Views: 452
Reputation: 9
use App\Http\Controllers\CustomerController;
use capital letter for starting of controller name
then
Route::resource('customers', CustomerController::class);
Upvotes: 1