多个中间件到Laravel 5路由不起作用

时间:2017-11-28 10:56:42

标签: php laravel authentication laravel-5 laravel-middleware

我希望以“" admin"”登录时查看信息中心和"珠宝商"。 当我登录" admin"它重定向到仪表板,当我作为珠宝商登录时,它不会进入仪表板。 我有两种类型的管理员和表格..." admin"," jeweler"

在auth.php中,我创建了两种类型的提供者

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'jeweler' => [
            'driver' => 'session',
            'provider' => 'jewelers',
        ],
    ],
 'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
        'jewelers' => [
            'driver' => 'eloquent',
            'model' => App\Jeweler::class,
        ],
    ],

我宣布了一个名为" AdminAuthenticated"的中间件。 ,在AdminAuthenticated.php中我们有

class AdminAuthenticated
{
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return $next($request);
        }
        return redirect()->route('admin.login');
    }
}

在web.php中我们有

Route::group(['prefix' => 'admin','middleware'=>
  ['adminAuth:admin','adminAuth:jeweler']], function () {
});

在核心我们有

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

        'adminAuth' => \App\Http\Middleware\AdminAuthenticated::class,
    ];

2 个答案:

答案 0 :(得分:3)

class AdminAuthenticated
{
    public function handle($request, Closure $next, ...$guards)
    {
        if ($guards) {

            foreach ($guards as $guard) {
                if (!Auth::guard($guard)->check()) {
                    return redirect()->route('admin.login');
                }
            }

            return $next($request);
        }
        return redirect()->route('admin.login');
    }
}

使用

Route::group(['prefix' => 'admin','middleware'=>
  ['adminAuth:admin,jeweler']], function () {
});

答案 1 :(得分:0)

你可以这样做来检查其中一个守卫:

class AdminAuthenticated
{
    public function handle($request, Closure $next, $guard = null)
    {
        if (func_num_args() == 2 && \Auth::check()) { //Default guard
             return $next($request);     
        }  
        for ($i = 2; $i < func_num_args();$i++) {
             if (Auth::guard(func_get_arg($i))->check()) {
                 return $next($request);
             }
        }
        return redirect()->route('admin.login');
    }
}

如果其中一个指定的守卫通过,或者当没有参数时默认守卫通过,则此中间件将通过。

您可以像以下一样使用它:

Route::group([ 
      'prefix' => 'admin',
      'middleware' => 'adminAuth:admin,jewler' 
    ], function () { ...