duandian8251 2017-04-27 16:11
浏览 18
已采纳

用laravel分隔用户类型的视图

I am making a project and the table Users haver a column called "tipo de usuario" (type of user in spanish) so I need that one kind of user (client) can't acces to some views that the other user (staff) does, in laravel I am using this in my controllers:

public function __construct(){

    $this->middleware('auth');

}

That is working so only logged users can acces, but then if a client try to acces to a view that should be only for my staff then he can do it because they both are logged users.

  • 写回答

1条回答 默认 最新

  • doulou1989 2017-04-27 16:23
    关注

    Assuming that you have different roles for the users.

    You can create another middleware using the artisan command:

    php artisan make:middleware <name>
    

    Then, on the handle method, you can check the role of the user

    Something like this:

    public function handle($request, Closure $next)
    {
        if (auth()->user()->role !== 'staff') {
            // Response if not staff
        }
    
        return $next($request);
    }
    

    Don't forget to register the middleware on the kernel (app/http/)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料