2015-11-22 40 views
2

我正在創建一個網站,用戶可以在其中啓動之前進行註冊。但是,當具有特定電子郵件地址的人登記時,他們已經可以訪問整個網站,其他人則被列入等待名單。Laravel 5.1 - 在每個頁面上重定向特定用戶

因此,讓我們假設每個使用Gmail地址的人都可以訪問,並且我希望其他人可以重定向到頁面/等待列表。


實施例:

Gmail使用者:

Route::get('favorites', '[email protected]'); 

Hotmail的用戶:

Route::get('favorites', '[email protected]') 

- >讓這個重定向到

Route::get('waitlist', '[email protected]') 

有一個簡單的,短的方式來爲每路線做到這一點,例如與如果,這樣的嗎?

if(substr($user->email, -9)!= "@gmail.com") 
{ 
    always redirect my routes to "Waitlist" 
} 
+0

我會在你的星座,在功能上做到這一點的邏輯。 :) – ihue

+0

如果您需要幫助,請告訴我。發佈您的登錄功能。我會幫你。 – ihue

+0

@ihue但如果用戶手動給出的url? – Nicolas

回答

1

好吧,感謝用戶@ihue我知道解決方案。

我必須創建一個middelware,每次輸入URL時都會檢查用戶的電子郵件。

這裏是我是如何做到的


我產生了中間件使用

php artisan make:middleware EmailRedirectMiddleware 

然後在中間件我編寫我的,如果循環。

<?php 

namespace App\Http\Middleware; 

use Auth; 
use Closure; 

class EmailRedirectMiddleware 
{ 
    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 
     $user = Auth::User(); 
     if(substr($user->email, -9) != "@gmail.com") 
     { 
      return redirect('waitinglist'); 
     } 

     return $next($request); 
    } 
} 

然後,只需在您的routes.php,使用中間件,你希望每一個路線,像這樣。

Route::group(array('middleware' => 'auth'), function(){ 
    Route::group(array('middleware' => 'App\Http\Middleware\EmailRedirectMiddleware'), function() { 
     Route::get('work', '[email protected]'); 
    }); 
}); 

第一行檢查是否用戶已登錄,第二行檢查的電子郵件的登錄的用戶擁有。 如果用戶沒有使用Gmail地址,他將被重定向到waitinglist,就像我在中間件中編碼的

+0

我很高興,它適合你。 :) – ihue

0

您可以在以下功能中更改多用戶的用戶重定向。 但是,在此處更新您的更改可能會被取消。

去的文件路徑和改變功能

供應商\ laravel \框架的\ src \照射\基金會\驗證\ AuthenticatesUser。PHP

protected function handleUserWasAuthenticated(Request $request, $throttles) 
    { 
     if ($throttles) { 
      $this->clearLoginAttempts($request); 
     } 

     if (method_exists($this, 'authenticated')) { 
      return $this->authenticated($request, Auth::user()); 
     } 

     $user = Auth::user(); 
     $type = $user->Rtype; 

     if($type==1){ 
      return redirect()->intended('/admin/dashboard'); 
     }elseif($type==2){ 
      return redirect()->intended('/admin/dashboard'); 
     }elseif($type==3){ 
      return redirect()->intended('/'); 
     }else{ 
     return redirect()->intended($this->redirectPath()); 
     } 

    }