Laravel 5路由的多个中间件不起作用
Posted
技术标签:
【中文标题】Laravel 5路由的多个中间件不起作用【英文标题】:multiple middleware to Laravel 5 route not working 【发布时间】:2018-05-11 19:23:48 【问题描述】:我想在以“admin”和“jeweller”身份登录时查看仪表板。 当我登录“管理员”时,它会重定向到仪表板,当我以珠宝商身份登录时,它不会进入仪表板。 我有 2 种类型的管理员和表格,即..“管理员”、“珠宝商”
在 auth.php 中,我创建了 2 种类型的提供者
'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.php 中声明了一个名为“AdminAuthenticated”的中间件
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,
];
【问题讨论】:
您可以在$middlewareGroups
数组中添加中间件组,例如'admin' =>['adminAuth:admin','adminAuth:jeweler']
并将您的组中间件设置为 admin
你应该看看 \Illuminate\Auth\Middleware\Authenticate
中间件并比较你自己的 AdminAuthenticated
中间件,如果它们都在执行相同的身份验证逻辑。
@apokryfos .. 我试过这个,但结果是一样的,不能用珠宝商登录
如果您需要两个中间件之一通过,那么您应该将它们组合成一个AuthWithAdminOrJewler
中间件。 Laravel 没有“或”中间件,都必须通过。
@apokryfos.. 谢谢你的工作
【参考方案1】:
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 ()
);
【讨论】:
我试过了,但结果一样,无法登录珠宝商 修改了你的答案...... if ($guards) $guards = explode('-', $guards); foreach ($guards as $guard) if (Auth::guard($guard)->check()) return $next($request); return redirect()->route('admin.login');和 root 到 ,'middleware'=>['adminAuth:jeweler-admin'] 如果用户未在所有提供的防护上进行身份验证,此中间件将失败。我认为问题在于 OP 希望用户至少在一名警卫上进行身份验证。【参考方案2】:您可以这样做来检查其中一名守卫:
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 () ...
【讨论】:
if ($guards) $guards = explode('-', $guards); foreach ($guards as $guard) if (Auth::guard($guard)->check()) return $next($request); return redirect()->route('admin.login');和 root 到 ,'middleware'=>['adminAuth:jeweler-admin'] 这也可以,但是如果你用逗号分隔它们,那么它们将作为单独的参数而不是单个参数传递给中间件,并且将在 func_get_args() 数组中以上是关于Laravel 5路由的多个中间件不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Route::currentRouteAction 在 laravel 5.4 中不起作用