You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在使用用户与管理员双中间件时让Blade模板对两者均可见?

Solution for Making Blade Templates Accessible to Both Users and Admins

First, let's confirm you've got your authentication guards set up correctly in config/auth.php (this is foundational for the rest to work):

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],
    'admins' => [
        'driver' => 'eloquent',
        'model' => App\Models\Admin::class,
    ],
],

If you need this access logic across multiple controllers, creating a dedicated middleware is the cleanest, most maintainable approach:

  1. Generate the middleware
    Run this Artisan command in your terminal:

    php artisan make:middleware AllowUserOrAdmin
    
  2. Update the middleware logic
    Open app/Http/Middleware/AllowUserOrAdmin.php and replace the handle method with this:

    public function handle($request, Closure $next)
    {
        // Check if either a regular user or admin is authenticated
        if (auth('web')->check() || auth('admin')->check()) {
            return $next($request);
        }
    
        // Redirect unauthenticated users to your preferred login page
        return redirect()->route('login');
    }
    
  3. Register the middleware
    Add this entry to the $routeMiddleware array in app/Http/Kernel.php:

    'allow.user.or.admin' => \App\Http\Middleware\AllowUserOrAdmin::class,
    
  4. Use the middleware in your controller
    Replace your existing middleware call in the controller constructor:

    public function __construct()
    {
        $this->middleware('allow.user.or.admin');
    }
    

Option 2: Closure Middleware (Quick One-Off Solution)

If you only need this access rule for a single controller, you can use an inline closure instead of creating a separate middleware file:

public function __construct()
{
    $this->middleware(function ($request, $next) {
        // Allow access if either guard has an authenticated user
        if (auth('web')->check() || auth('admin')->check()) {
            return $next($request);
        }

        return redirect()->route('login');
    });
}

Bonus: Differentiate Content in Blade (If Needed)

If you want to show specific content to regular users vs admins while keeping the template accessible to both, add this to your Blade file:

@if(auth('web')->check())
    <div class="user-greeting">Welcome, regular user!</div>
@elseif(auth('admin')->check())
    <div class="admin-greeting">Welcome, admin! Here's your dashboard shortcut</div>
@endif

Key Notes

  • Make sure your login routes for both guards are properly set up (e.g., admin/login with a named route like admin.login if you want to redirect unauthenticated admins to their own login page).
  • Use auth('guard-name')->user() if you need to access the authenticated user's details in your controller or Blade template.

内容的提问来源于stack exchange,提问作者Mosharof Hossain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:31:15