【发布时间】:2017-07-13 19:38:36
【问题描述】:
我有一个用于管理员登录的中间件,我正在检查用户是否是管理员。但现在我想检查管理员是否有权访问该页面。我该怎么做?
我的管理中间件是:
public function handle($request, Closure $next)
{
if(Auth::check())
{
$user = Auth::user();
if($user->user_type=='employee')
{
return $next($request);
}
else
{
return redirect('/');
}
}
else
{
return redirect('/');
}
}
一种方法是将以下代码添加到每个控制器的每个功能中。
if(Auth::user()->permission=='manage_employee'){
//code here
}
else
{
//redirect to access denied page
}
但这不是正确的方法并且耗时。有没有其他不使用包的方法?
【问题讨论】:
-
你应该在你的路由文件中保护它们。laravel.com/docs/5.4/authentication#protecting-routes
-
看看Gates。
-
@Daan 我已经在使用管理中间件了。但现在我想检查这些管理员的权限。