2013-06-25 37 views
1

我使用Laravel 3兩套登錄控制器 - 主域名進入登錄,所有子域應該途徑門戶/登錄@指數Laravel 3 - 路由集合dyncamically

我使用下面的代碼我routes.php文件:

Route::filter('before', function() 
{ 
    $server = explode('.', Request::server('HTTP_HOST')); 
    if (count($server) == 3) 
    { 
     $account = Account::where('subdomain', '=', $server[0])->first(); 
     Session::put('account_id', $account->id); 
     Route::get('login', '[email protected]'); 
     Route::post('login', '[email protected]'); 
     Route::get('logout/(:any)', '[email protected]'); 
    } 
    else 
    { 
    // some other stuff - no routing calls in here 
    } 
} 

此代碼工作正常捕獲子域&做其他任務(如設置$ ACCOUNT_ID),但似乎沒有對路由

test.mydomain影響。 com/login應該去portal/logi n,而是轉到主登錄控制器。

我已經通過搜查,以確保有沒有影響這個過濾器(它是一種遺傳性的應用程序)

這是設置此正確的方式,如果是這樣,還有什麼可以成爲影響呢?

TIA!

回答

0

這是因爲當你在裏面

if (count($server) == 3) 
{ 
    // Here 
} 

使用get/post新航線,登記是不會迴應,因爲系統已經完成路徑一致,在這種情況下,你可以將請求轉發到一個新的使用

Route::forward($method, $uri); 

路線這是laravel/routing/route.php文件nelow

/** 
* Calls the specified route and returns its response. 
* 
* @param string $method 
* @param string $uri 
* @return Response 
*/ 
public static function forward($method, $uri) 
{ 
    return Router::route(strtoupper($method), $uri)->call(); 
} 
給出3210

所以,如果你想創建一個類似於Route::get('login', '[email protected]');的請求,那麼你可以做到這一點作爲

路線::向前(「GET」,「登錄」); 在這種情況下,您只需註冊一條路線即可保持此路線的登記。因此,註冊/添加要動態地創建和使用Route::forward()方法內部

if (count($server) == 3) 
{ 
    Route::forward('GET', 'login'); // for a get request 
    Route::forward('POST', 'login'); // for a post request 
} 

這就是它在routes.php請求。

+0

謝謝,我會看看。不過,我不相信這是因爲它在count($ server)中;這是因爲它在之前的過濾器中。我將它移動到正常路由並且它正常工作。您的答案似乎基本正確,但 - 我會調查並通知您 – jmadsen

+0

@jmadsen,歡迎您,並等待... –