且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 4 Auth始终重定向到登录页面吗? /登录

更新时间:2023-12-03 23:01:46

filters.php文件中有一个默认的auth过滤器,应该是这样的:

There is a default auth filter in the filters.php file, it should be like this:

Route::filter('auth', function($route, $request)
{
    if (Auth::guest()) return Redirect::guest('login'); // /login url
});

此过滤器(如上所示)将检查用户是否未登录,然后将发生重定向,并将用户发送到/login url,否则将不会发生任何事情,用户将被发送到请求的页面.

This filter (given above) will check if the user is not logged in then a redirect will occur and user will be sent to /login url and otherwise nothing will happen, user will be sent to the requested page.

此外,默认情况下,以下filter可用,并且此filter仅检查用户是否已登录,然后默认情况下会将其重定向到/(主页):

Also, following filter is available by default and this filter just checks if the user is already logged in then (s)he will be redirected to / (home page) by default:

Route::filter('guest', function($route)
{
    if (Auth::check()) return Redirect::to('/'); // you may change it to /admin or so
});

此(guest)过滤器与/login一起使用,如下所示,因此,如果已登录的用户打算登录,则默认情况下,该用户将被重定向到主页:

This (guest) filter is used with /login as given below, so if a logged in user intended to log in then the user will be redirected to home page by default:

Route::get('login', array('before' => 'guest', 'uses' => 'UsersController@getLogin'));

现在,在您的routes.php文件中,您声明了以下路由:

Now, in your routes.php file you have following route declared:

Route::group(array('before' => 'auth'), function()
{
    Route::controller('admin', 'UsersController');
});

如果一切正常,则此设置应该可以使用.如果注销的用户尝试访问admin,则该用户将被发送到login,并且默认情况下这些用户可用并且应该可以使用.

If everything is fine then this setup should work. If a logged out user tries to visit admin then user will be sent to login and these are by default available and should work.