Laravel 5.2- 限制用户访问
Laravel 5.2- Restrict user acess
我正在做一个 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?
我读过一些关于中间件的内容,但老实说,我不确定如何使用它。
我认为这里不需要中间件,只需这样做
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'));
}
我正在做一个 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? 我读过一些关于中间件的内容,但老实说,我不确定如何使用它。
我认为这里不需要中间件,只需这样做
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'));
}