简体   繁体   English

如何在Laravel 5中将参数从路由器传递到中间件?

[英]How to pass arguments from router to middleware in 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...
});

and inside the middleware: 在中间件中:

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

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

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