doubei8541 2016-08-28 08:49
浏览 58
已采纳

在Laravel中进行身份验证时如何检查用户表中的参数?

I am trying to make a Laravel website that uses Laravel's Auth package. I'm using Laravel 5.3.2.

I have created a field in user table called role.

Now I want to know how to check the users role during the authentication process and then redirect to a required view based on the role. Please help me figure out how this would be possible.

Thank you very much in advance.

  • 写回答

2条回答 默认 最新

  • duanju8308 2016-08-28 09:10
    关注

    When a user logs in, this is done through your LoginController.php which is located at app\Http\Controllers\Auth

    This controller uses a trait called AuthenticatesUsers.

    This trait has a method called authenticated() which by default is empty. This method is called if it's not empty by the trait - after all the necessary loggin in stuff has been done.

    You could override this method in your AuthenticationController.php and add the functionality you are asking for. An example would be:

    // You actually get an Auth\User object passed to you by the trait!
    public function authenticated(Request $request, $user)
    {
        if($user->role == 'admin') {
            // You could do anything here
            return redirect()->route('admin-dashboard');
        } else {
            return redirect()->route('home');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题