简体   繁体   English

Laravel无法通过多个中间件

[英]Laravel cannot pass multiple middleware

recently i created two middlewares which is one for user called device, and one other for super user which is high level of admin. 最近,我创建了两种中间件,一种用于设备的用户,另一种用于高级用户,其具有较高的管理员权限。 This is my middleware 这是我的中间件

Role Device Middleware 角色设备中间件

<?php

namespace App\Http\Middleware;

use Closure;

class RoleDevice
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check() && Auth::User()->role=='device'){
            return $next($request);
        }
        return redirect()->route('login')->with('danger',"You don't have an access");
    }
}

Role Device Super User 角色设备超级用户

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use User;
class RoleSuper
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check() && Auth::User()->role=='super'){
            return $next($request);
        }
        return redirect()->route('login')->with('danger',"You don't have an access");
    }
}

after i created the middlewares, i put into the routes which is one route could access two middlewares. 创建中间件之后,我将其放入路由中,一条路由可以访问两个中间件。 Here is one of my route. 这是我的路线之一。

Route::get('/dashboard','DashboardController@index')->middleware(['rolesuper','roledevice'])->name('dashboard');

and when i try to log in into my website, it returns 当我尝试登录到我的网站时,它返回

You don't have an access

which is don't pass into the middleware. 这不会传递给中间件。

i hope i get any comments above ! 我希望我能收到以上任何评论! thanks. 谢谢。

Middlewares are executed in the order the are passed. 中间件按照传递的顺序执行。 So in case first middleware returns redirect response that's it - second middleware won't be executed. 因此,如果第一个中间件返回的只是重定向响应-第二个中间件将不会执行。

You could combine both middleware into one and pass available roles as middleware parameter or just create single middleware for this that will verify if user is authorized. 您可以将两种中间件组合为一个,并通过可用角色作为中间件参数,或者为此仅创建一个中间件,以验证用户是否被授权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM