如何在Laravel 5中将参数从路由器传递到中间件? [英] How to pass arguments from router to middleware in laravel 5?

查看:461
本文介绍了如何在Laravel 5中将参数从路由器传递到中间件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天,当我尝试将参数从路由器传递到中间件时,我遇到了一个问题,以检查经过身份验证的用户是否具有访问该路由的权限. 如何从路由传递参数到中间件?

I was facing an issue these days when I tried to pass arguments from my router to my middleware, to check if the authenticated user has the permissions to access that route. How can I pass an argument from routes to the middleware?

推荐答案

我尝试这样做,它对我来说非常好: 在我的路线文件中:

I tried to do it and it works very well for me: In my routes files:

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.
    }

这篇关于如何在Laravel 5中将参数从路由器传递到中间件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆