Laravel中间件获取路由参数 [英] Laravel middleware get route parameter

查看:413
本文介绍了Laravel中间件获取路由参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写学校俱乐部管理系统"之类的代码,并遇到有关资源权利授权的问题.

I am coding some thing like 'school club manage system' and meet some problem on rights authorization of resource .

假设有club,并且club有经理,并且我想在用户可以使用中间件进行管理之前,检查用户是否为club的经理.

Suppose there are club , and club have manager , and i want to check if the user is a manager of club before he can manage it with a middleware.

使用laravel 5.2

Using laravel 5.2

我的router看起来像这样:

Route::resource('club', 'ClubController');

我创建的中间件看起来像这样:

The middleware I create looks like that :

  public function handle($request, Closure $next)
    {
        if (!Auth::check()){
            // ask user to login
        }

        $club = Club::findOrFail($request->input('club'));
        $user = Auth::user();

        if (!$club->managers->contains($user)){
            //tell user your are not manager .
        }

        return $next($request);
}

但是我没能从requests那里获得club的ID.

But I failed to get the id of club from requests .

我该如何解决问题?

谢谢.

推荐答案

如果要查找url参数,则从中间件内部的laravel 5.2中的资源路由获取该参数的最佳方法如下.假设您要获取ID参数形式,网址为club/edit/{id}

if you are looking for the url parameters the best way of getting that from the resource routes in laravel 5.2 inside the middleware is below. Lets say you want to get id parameter form url club/edit/{id}

$request->route()->parameter('id');

这篇关于Laravel中间件获取路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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