2

我正在自定义 laravel 5 的内置登录,以便它根据我添加到用户表的类型列重定向到三个不同的路径,我尝试更改 RedirectIfAuthenticated 中间件的句柄函数。但它似乎总能找到主 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 的新手,我非常感谢任何帮助和解释

4

2 回答 2

1

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

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

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

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

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

现在您只需要名为patient/dashboard.blade.php,doctor/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');

然后在这些控制器方法中做任何你需要做的事情。

于 2015-10-19T04:22:15.223 回答
0

查看身份验证部分中的文档

基本上你需要的是:

  • 在app/Http/routes.php创建 auth 路由:

    // 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包。

于 2015-10-18T17:17:54.643 回答