2

我自定義laravel 5的內置登錄,以便根據類型列將其重定向到三個不同的路徑,我添加到users表中,我試着改變RedirectIfAuthenticated的句柄函數中間件。但它似乎總是找到家庭URI。Laravel 5自定義內置登錄重定向

這裏是我的編輯中間件

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,我會非常感謝所有幫助和解釋

回答

1

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', '[email protected]'); 
Route::get('home/doctor', '[email protected]'); 
Route::get('home/nurse', '[email protected]'); 

然後在這些控制器方法中做任何你需要的。

+0

非常感謝你:)這對我很有用。 –

0

查覈在Authentication section

基本上你需要的是文檔:

  • 創建auth路由a噸應用程序/ HTTP/routes.php文件

    ​​
  • 創建登錄表單視圖:

    <!-- 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> 
    
  • 手動驗證用戶應用程序/ HTTP /控制器/認證/ 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'); 
          } 
         } 
        } 
    } 
    
  • 或者如果你想保持RedirectIfAuthen下的邏輯ticated中間件你可能只是解決您的代碼:

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

+0

其仍在使用其內置..我不知道在哪裏編輯它的內置函數 –