如何从路由器传递参数在laravel 5到中间件?(How to pass arguments fr

2019-10-23 04:45发布

我这几天面临一个问题,当我试图从我的路由器参数传递给我的中间件,以检查身份验证的用户有权限访问路线。 我如何可以传递路由到中间件的说法?

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


文章来源: How to pass arguments from router to middleware in laravel 5?
标签: php laravel-5