Laravel 5 自定义内置登录重定向

Laravel 5 customizing built in login redirect

我正在自定义 laravel 5 的内置登录,以便根据我添加到用户的 type 列重定向到三个不同的路径 table,我尝试更改 RedirectIfAuthenticated 中间件的句柄函数。但它似乎总能找到 home URI。

这是我编辑的中间件

public function handle($request, Closure $next)
    {
        if ($this->auth->check() && $this->auth->user()->type == 'patient') {
            // return redirect('/home');
            return 'PATIENT VIEW';
        } elseif ($this->auth->check() && $this->auth->user()->type == 'doctor') {

            return 'DOCTOR VIEW';

        } elseif ($this->auth->check() && $this->auth->user()->type == 'nurse') {

            return 'NURSE VIEW';
        }

        return $next($request);
    }

我是 laravel 5 的新手,非常感谢任何帮助和解释

查看 Authentication section

中的文档

基本上你需要的是:

  • app/Http/routes 创建授权路由。php:

    // Authentication routes...
    Route::get('auth/login', 'Auth\AuthController@getLogin');
    Route::post('auth/login', 'Auth\AuthController@postLogin');
    
  • 创建登录表单视图:

    <!-- resources/views/auth/login.blade.php -->
    <form method="POST" action="/auth/login">
    {!! csrf_field() !!}
    
    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>
    
    <div>
        Password
        <input type="password" name="password" id="password">
    </div>
    
    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>
    
    <div>
        <button type="submit">Login</button>
    </div>
    </form>
    
  • 手动验证用户 app/Http/Controllers/Auth/AuthController.php:

    <?php
    
    namespace App\Http\Controllers;
    
    use Auth;
    use Illuminate\Routing\Controller;
    
    class AuthController extends Controller
    {
        /**
         * Handle an authentication attempt.
         *
         * @return Response
         */
        public function authenticate()
        {
            if (Auth::attempt(['email' => $email, 'password' => $password])) {
                if (Auth::user()->type == 'patient'){
                    return redirect()->intended('patientDashboard');
                }
                if (Auth::user()->type == 'doctor'){
                    return redirect()->intended('doctorDashboard');
                }
            }
        }
    }
    
  • 或者,如果您想将逻辑保留在 RedirectIfAuthenticated 中间件下,您可以修改您的代码:

    public function handle($request, Closure $next)
    {
    if ($this->auth->check())
    {
        //we have a logged user check if it's patient
        if($this->auth->user()->type == 'patient'){
    
          return new RedirectResponse(url('/patient'));
    
        }else if($this->auth->user()->type == 'doctor'){
    
          return new RedirectResponse(url('/doctor'));
    
        }
    }
    
    return $next($request);
    }
    

你也应该看看这个 Entrust 包。

RedirectIfAuthenticated 在这里被滥用了。该中间件适用于经过身份验证的用户尝试访问仅应由访客访问的页面时。例如,如果我是用户并且我尝试查看登录或注册表格,它不会让我。

我不会弄乱身份验证本身......其中一些很容易定制,但你想要做的却不是。我会让 Laravel 先对它们进行身份验证,然后处理之后的操作。

/home 是用户登录时转到的默认路由。将您的 if 检查移至该路由控制器方法。更好的是......如果你设置正确,你根本不需要任何检查。

class HomeController {
    public function index()
    {
        $user = \Auth::user();

        return view($user->type . '.dashboard');
    }
}

现在您只需要名为 patient/dashboard.blade.phpdoctor/dashboard.blade.php 等的视图。如果您有更复杂的逻辑,那么您可能需要一个实际的重定向

        return redirect('home/' . $user->type);

为每种类型定义路由

Route::get('home/patient', 'PatientController@dashboard');
Route::get('home/doctor', 'DoctorController@dashboard');
Route::get('home/nurse', 'NurseController@dashboard');

然后在这些控制器方法中执行您需要的任何操作。