laravelrouteslaravel-9

Target class [admin] does not exist


Having problem with Laravel 9. Simple website is working fine. But when I access admin panel I get this error after logging in .I want to create 2 pages, namely the admin and user pages, but I am having problems with routes that are not readable

AuthController

 public function loginAksi(Request $req)
    {
        if ($req->email == null or $req->password == null) {
            return redirect()->back()->with(['error' => 'Masukkan Email atau Password !']);
        } else {
            if (Auth::attempt([ 'email' => $req->email, 'password' => $req->password ])) {
                $user = Auth::user();
                if($user->level == 1){
                    return redirect()->route('admin');
                } else {
                    return redirect()->route('user');
                }
            } else {
                return redirect()->back()->with(['error' => 'Login Gagal !']);
            }
        }
    }

Tampilan Route Saya web.php

Route::get('welcome', function () {
    return view('welcome');
});

Route::controller(AuthController::class)->group(function () {

    Route::get('login', 'login')->name('login');
    Route::post('login', 'loginAksi')->name('login.aksi');

    Route::get('logout', 'logout')->middleware('auth')->name('logout');
});


Route::middleware('auth')->group(function () {

    Route::middleware(['admin'])->group(function(){
        
        Route::get('', [DashboardController::class, 'index'])->name('admin.dashboard');

        Route::controller(IbuController::class)->prefix('users')->group(function () {
        Route::get('', 'index')->name('ibu');
        Route::get('tambah', 'tambah')->name('admin.ibu.tambah');
        Route::post('tambah', 'store')->name('admin.ibu.store');
        Route::get('edit/{id}', 'edit')->name('admin.ibu.edit');
        Route::post('edit/{id}', 'update')->name('admin.ibu.update');
        Route::get('hapus/{id}', 'hapus')->name('admin.ibu.hapus');
        });
    });

    
    Route::middleware(['user'])->group(function(){
        Route::get('user', function (){
            return view('user.dashboard');
        })->name('user');
    });
    
});

can anyone help me solve this?


Solution

  • The "target class [class] does not exist" is the error message thrown when a middleware by alias is not found. This means that this line Route::middleware(['admin'])->group(function(){ is referencing an alias that simply is not defined in the Kernel.

    You can add this alias by adding your alias to the $routeMiddleware property in the App\Http\Kernel.php (as stated in the docs):

    protected $routeMiddleware = [
        // 
        'admin' => YourMiddlewareClass::class,
    ];
    

    However, as @aceraven777 stated, you'll probably get a follow-up exception seeing as there's no route defined named 'admin'.