2015-04-27 106 views
2

對於我的web應用程序,我們爲每個帳戶account1.example.org & account2.example.org有子域。他們都去同一個地方,只要它允許我們在URL中擁有帳戶。Laravel 5登錄重定向到子域

我有一切工作與子域。我的問題是與Laravel 5登錄代碼。我不確定如何讓它重定向到子域(存儲在數據庫中,可以通過Auth :: user() - > account-> sub_domain訪問)。

我看到有人嘗試並更改操作但未添加到地址。做這個的最好方式是什麼?

回答

3

定義route -

Route::get(['domain' => '{account}.myapp.com'], function($account){ 
    // Process data or redirect 
}); 

並登錄後 -

Redirect::to('account1.myapp.com'); 
+0

我可能會用第一部分的Route :: group來包含所有的路由。但是,重定向會轉到哪裏? –

+0

我目前的想法是重寫postLogin,就像我對getRegister所做的一樣,然後改變鏈接。 –

+0

只需添加一個方法來處理這些子域的請求。 –

0

所以從SGT BOSE的幫助,這是我想出了。

在我AuthController.php我加

/** 
* Redirect Path 
* 
* @return \Illuminate\Http\RedirectResponse 
*/ 
public function redirectPath() 
{ 
    $redirectTo = property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; 

    $url = Config::get('app.url'); 
    $subDomain = Auth::user()->account->sub_domain; 
    $scheme = parse_url($url, PHP_URL_SCHEME); 
    $host = parse_url($url, PHP_URL_HOST); 
    return $scheme.'://'.$subDomain.'.'.$host.$redirectTo; 
} 

這樣做是返回的鏈接postLogin和的postRegister。此方法僅在成功時調用。獲取我的子域並根據我的URL的配置項目建立一個鏈接。

爲確保登錄成功,我還必須更改config/session.php文件中的域項。 'domain' => null -> 'domain' => '.example.org'