2014-04-08 33 views
0

相同的標題,我用我的莊網頁時希望所有的人,必須登錄(看起來像FB或Twitter,...)與一些要求如下:過濾器使用的網站之前登錄 - Laravel框架

  • 如果當前URL是'/'(主頁),則系統顯示已註冊的接口。 (顯示而不是重定向)

  • 如果如不同的URL「/」(主頁),系統重定向到登錄頁面。

有人可以幫我嗎?我正在使用laravel框架。

+1

如果你只是谷歌'laravel filter',第一個結果就是你所需要的東西。 – hndr

回答

0

Laravel使用名爲電源濾波器的事情。

您可以在任何途徑使用它們:: 行動你想要的。

不過有點爲例五月幫助你。

按照您的要求:

// Check manualy if user is logged. If so, redirect to the dashboard. 
// If not, redirect to the login page 
Route::get('/', function() 
{ 
    if (Auth::check()) // If user is logged 
     return View::make('dashboard') 
    return View::make('/login'); 
} 

// Each routes inside this Route::group will check if the user is logged 
// Here, /example will only be accessible if you are logged 
Route::group(array('before'=>'auth', function() 
{ 
    // All your routes will be here 
    Route::get('/example', function() 
    { 
    return View::make('contents.example'); 
    } 
}); 

當然,過濾器AUTH是建立在Laravel。你可以在app/filters.php 找到這個文件,並根據你的需求進行修改。 如下:

Route::filter('auth', function() 
{ 
    if (Auth::guest()) return Redirect::guest('/login'); 
}); 
+0

如果某些評論(that_guy)或答案(我)不...回答你的問題,不要忘記接受一種或另一種讓人們知道有一個解決方案。 – ChainList