1

I'm new to Laravel ( version 3 ), i do not know how to set Route and filters in Laravel so that any unauthorized user that is trying to access any url redirects to the login page (NOT the 404 error), in another word the default home page for unauthorized users is going to be the login page and for the authorized users it's going to be the dashboard.

4

2 回答 2

3

如果你使用 laravel Auth 类,你可以创建一个授权的路由组。如果用户未登录,所有在此处定义的路由都将被重定向。您的路由器文件将如下所示:

Route::get('/', array('as' => 'intro', 'uses' => 'intro@index'));
Route::get( 'login', array('as' => 'login', 'uses' => 'user@login'));
Route::get( 'logout', array('as' => 'logout', 'uses' => 'user@logout'));

// PROTECTED
Route::group(array('before' => 'auth'), function()
{
   Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'user@dashboard'));
});

// AUTH FILTER
Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::to('login');
});
于 2013-04-21T13:19:49.013 回答
0

只需在这样的路由声明中添加一个前置过滤器

Route::get('edit_profile', array('before' => 'auth', function()
{
    return View::make('profile.edit');
}));

Laravel 中默认存在 Auth 过滤器。

于 2013-04-21T16:15:16.773 回答