📜  如何将参数传递给中间件 laravel 8 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:54:51.592000             🧑  作者: Mango

代码示例1
Generally you can pass parameters to middleware via using : symbol like this:

Route::get('user/{id}', ['middleware' => 'auth:owner', function ($id) {
    // Your logic here...
}]);
And get the passed parameter into middleware method like this:

check() && auth()->user()->hasRole($role)) {
            return $next($request);
        }

        return redirect('login');
    }
}
Note that the handle() method, which usually only takes a $request and a $next closure, has a third parameter, which is our middleware parameter.

If you passed in multiple parameters like auth:owner,subscription to your middleware call in the route definition, just add more parameters to your handle method which will look like this - handle($request, Closure $next, $role,$subscription)