I am trying to create a login system where both admin and users can log in using the same form. The problem is when I log in with admin credentials, the URL redirects correctly to admin/dashboard
, but I see the error:
Target class [admin] does not exist
Here is the relevant code:
Admin Controller
<?php
// appHttpControllersAdminAdminController.php
namespace AppHttpControllersAdmin;
use AppHttpControllersController;
use IlluminateHttpRequest;
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('admin');
}
public function dashboard()
{
return view('admin.dashboard');
}
}
User Dashboard Controller
<?php
// appHttpControllersDashboardController.php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class DashboardController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$user = auth()->user();
$recentOrders = $user->cartItems()
->with('product')
->latest()
->take(5)
->get();
return view('dashboard', compact('recentOrders'));
}
}
Authenticated Session Controller
<?php
// appHttpControllersAuthAuthenticatedSessionController.php
namespace AppHttpControllersAuth;
use AppHttpControllersController;
use AppHttpRequestsAuthLoginRequest;
use IlluminateHttpRedirectResponse;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateViewView;
class AuthenticatedSessionController extends Controller
{
public function create(): View
{
return view('auth.login');
}
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
if (Auth::user()->isAdmin()) {
return redirect()->intended(route('admin.dashboard'));
}
return redirect()->intended(route('dashboard'));
}
public function destroy(Request $request): RedirectResponse
{
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/');
}
}
User Model
<?php
// appModelsUser.php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateFoundationAuthUser as Authenticatable;
use IlluminateNotificationsNotifiable;
use LaravelSanctumHasApiTokens;
class User extends Authenticatable
{
use HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
'is_admin',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'is_admin' => 'boolean',
];
public function cartItems()
{
return $this->hasMany(CartItem::class);
}
public function isAdmin()
{
return $this->is_admin;
}
}
Kernel Middleware
<?php
// appHttpKernel.php
namespace AppHttp;
use IlluminateFoundationHttpKernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
IlluminateHttpMiddlewareTrustProxies::class,
IlluminateHttpMiddlewareHandleCors::class,
IlluminateFoundationHttpMiddlewarePreventRequestsDuringMaintenance::class,
IlluminateHttpMiddlewareValidatePostSize::class,
AppHttpMiddlewareTrimStrings::class,
IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
];
protected $middlewareGroups = [
'web' => [
AppHttpMiddlewareEncryptCookies::class,
IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
IlluminateSessionMiddlewareStartSession::class,
IlluminateViewMiddlewareShareErrorsFromSession::class,
AppHttpMiddlewareVerifyCsrfToken::class,
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
'api' => [
IlluminateRoutingMiddlewareThrottleRequests::class . ':api',
IlluminateRoutingMiddlewareSubstituteBindings::class,
],
];
protected $middlewareAliases = [
'auth' => AppHttpMiddlewareAuthenticate::class,
'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
'cache.headers' => IlluminateHttpMiddlewareSetCacheHeaders::class,
'can' => IlluminateAuthMiddlewareAuthorize::class,
'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
'password.confirm' => IlluminateAuthMiddlewareRequirePassword::class,
'signed' => IlluminateRoutingMiddlewareValidateSignature::class,
'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
'verified' => IlluminateAuthMiddlewareEnsureEmailIsVerified::class,
'admin' => AppHttpMiddlewareAdminMiddleware::class,
];
}
Routes
<?php
// routes/web.php
use AppHttpControllersAdminAdminController;
use AppHttpControllersDashboardController;
use IlluminateSupportFacadesRoute;
Route::get('/', [HomeController::class, 'index'])->name('home');
require __DIR__.'/auth.php';
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
});
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('/admin/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
});
I’ve confirmed that the middleware and route definitions seem fine. What am I missing here? How can I fix the Target class [admin] does not exist
error?
Any help or guidance would be greatly appreciated!
I am tired to find out where the problem is occur.