2014-03-06 59 views
0

我正在使用Laravel 4.當在會話中找到「logged_in_id」時,我希望它繼續通過波紋管書寫的路徑進行解析。Laravel重定向問題

Route::get('/{anything}', function($anything) 
{ 
    if(!Session::has('logged_in_id')) { 
     return View::make('user.login'); 
    } else { 
     //continue to check Route::get written bellow this Routing 
    } 
})->where('anything', '[A-Za-z0-9\/?=]+'); 

如果我寫Redirect::to('/'.$anything)然後進入同一條路線,並不斷在循環重定向。有什麼辦法可以解決這個問題嗎?

+0

則邏輯是錯誤的。你有沒有重定向? – Andreyco

回答

0

我會創建一個過濾器,然後將其應用於任何需要它的路由。

請記住首先放置更具限制性的路線,並使用更通用的路線。

參見例如:

Route::filter('logged_in', function() 
{ 
    if(!Session::has('logged_in_id')) { 
     return View::make('user.login'); 
    } 
}); 

Route::get('testing', array(
    'before' => 'logged_in', 
    function() 
    { 
     return View::make('user.testing'); 
    } 
)); 

Route::get('/{anything}', array(
    'before' => 'logged_in', 

    function($anything) 
    { 
     return View::make('user.anything'); 
    } 

))->where('anything', '[A-Za-z0-9\/?=]+');