Home > OS >  Target class does not exist. Routing Prefix Laravel 9
Target class does not exist. Routing Prefix Laravel 9

Time:10-10

I am learning Laravel 9, and I got a problem Target class [Admin\DashboardController] does not exist. when using prefix routing. Is someone can help me to fix it?

this code on routes/web:

Route::prefix('admin')->namespace('Admin')->group(function(){
    Route::get('/','DashboardController@index')->name('dashboard');
});

this code on App\Http\Controllers\Admin\DashboardController:

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function index(Request $request){
        return view('pages.admin.dashboard');
    }
}

CodePudding user response:

You've specified an incorrect namespace in your route. As per the error message:

Target class [Admin\DashboardController] does not exist

Laravel expects to find a DashboardController in the Admin namespace, however, you've defined your DashboardController with the namespace App\Http\Controllers\Admin.

Update the namespace on your route.

Route::prefix('admin')->namespace('App\Http\Controllers\Admin')->group(function(){
    Route::get('/','DashboardController@index')->name('dashboard');
});

CodePudding user response:

If you read the documentation, you will see that you must use another way:

Route::prefix('admin')->namespace('Admin')->group(function(){
    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
});
  • Related