doulong4169 2017-11-21 14:44
浏览 41

一个用于不同类型用户的登录页面,并重定向到Laravel中的预期仪表板

I've been planning to make one login page for different types of users and it will be able to direct them to their intended dashboards/homepages. These are my two users:

  1. Traveler
  2. Agent

The traveler can successfully login and will be directed to the intended homepage BUT the Agent wont be directed to its intended homepage instead it will be directed back to the login page.

This is my code of the Login Controller

<?php

namespace App\Http\Controllers\Auth;

use Auth;
use App\Travelers;
use App\Agents;
use App\Users;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function login(Request $request){
            if(Auth::attempt([
                'email' => $request->email,
                'password' => $request->password
            ])){   
                $user = Users::where('email', $request->email)->first();

                switch ($user->userType){
                    case 'traveler':
                        return redirect()->route('Traveler/HomePage');
                        break;
                    case 'travel agent':
                        return redirect()->route('Agent/HomePage');
                        break;
                }
            }
            else{
                return redirect()->route('login');
            }
    }

}
  • 写回答

2条回答 默认 最新

  • doulu4976 2017-11-21 15:22
    关注

    I think you need to redefine method redirectPath in LoginController For example:

    public function redirectPath() 
    {  
        if (Auth::user()->role == 'admin'){
            return '/admin';
        }
    
        return '/';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大