Muhammad Hassan Javed
Muhammad Hassan Javed

Reputation: 390

Using controller::class in Route::resource is causing namespace to be included two times resulting in Controller Not Found

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);

Solution

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.

Update

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

Answers (1)

Nitin Pujari
Nitin Pujari

Reputation: 9

use App\Http\Controllers\CustomerController;

use capital letter for starting of controller name

then

Route::resource('customers', CustomerController::class);

Upvotes: 1

Related Questions