我最近遇到了一个问题,当时我试图将参数从路由器传递到我的中间件,以检查经过身份验证的用户是否具有访问该路由的权限。 如何将路由中的参数传递给中间件?
答案 0 :(得分:1)
我试着这样做,对我来说效果很好: 在我的路线文件中:
Route::group(['prefix' => 'agenda', 'middleware' => 'auth', 'permissions' => 'user.create|user.delete'], function() {
//my routes here...
});
并在中间件内部:
class AuthMiddleware {
private $r;
private $guard;
public function __construct(Router $r, Guard $g)
{
$this->r = $r;
$this->guard = $g;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$route = $this->r->getCurrentRoute();
$action = $route->getAction(); //$action['permissions'] is the string received from the routes file.
}