0

我正在做一个 Laravel 5.2 项目,我有用户、旗帜和国家。我试图实现的是每个用户都可以单击“标志”菜单,它应该显示用户所在国家/地区的标志列表。

所以用户有country_id

标志有 country_id。

目前,我可以显示每个用户及其各自国家/地区的标志。

这是路线。

 Route::get('flags/{Country_id}','FlagController@showFlags');

风景

<a href="flags/{{Auth::user()->country_id}}">

和我的控制器

public function showFlags($id)
{

    $country = new Country;
    $country = $country->find($id);

    $flags = $country->flags;


    return view('layouts.f.mainf',compact('flags'));

}

问题是,如果我将 url 上的县 id 更改为其他任何内容,它将显示另一个国家/地区的标志,我如何限制它只能在用户国家/地区与 url 国家/地区 id 匹配时才能访问?我读过一些关于中间件的东西,但老实说我不知道​​如何使用它。

4

1 回答 1

1

我认为这里不需要中间件,只需这样做

public function showFlags($id)
{
    if($id != \Auth::user()->country_id)
    {
        throw new ProperException;
    }
    $country = new Country;
    $country = $country->find($id);

    $flags = $country->flags;
    return view('layouts.f.mainf',compact('flags'));
}
于 2016-03-21T21:34:58.083 回答