skip to Main Content

I am learning Laravel 9, and I got a problem Target class [AdminDashboardController] 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 AppHttpControllersAdminDashboardController:

namespace AppHttpControllersAdmin;

use AppHttpControllersController;
use IlluminateHttpRequest;

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

2

Answers


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

    Target class [AdminDashboardController] does not exist

    Laravel expects to find a DashboardController in the Admin namespace, however, you’ve defined your DashboardController with the namespace AppHttpControllersAdmin.

    Update the namespace on your route.

    Route::prefix('admin')->namespace('AppHttpControllersAdmin')->group(function(){
        Route::get('/','DashboardController@index')->name('dashboard');
    });
    
    Login or Signup to reply.
  2. 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');
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search