在使用网站之前过滤登录 - Laravel Framework

时间:2014-04-08 14:03:04

标签: php laravel

与标题相同,我希望所有人在使用我的trang web时,必须登录(看起来像FB或Twitter,......),其中一些要求如下:

  • 如果当前网址是' /' (主页),系统显示已注册的接口。 (显示而不是重定向)

  • 如果是其他网址' /' (主页),系统重定向到登录页面。

有人可以帮帮我吗?我正在使用laravel框架。

1 个答案:

答案 0 :(得分:0)

Laravel使用称为过滤器的电源。

您可以在任何Route :: 动作中使用它们。

但是一点点例子可能对你有帮助。

根据您的要求:

// 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'); 
});
相关问题