简体   繁体   English

Laravel 4 Auth重定向总是登录页面? /登录

[英]Laravel 4 Auth redirect always to login page? /login

I am using laravel for my web application and in routes.php I have: 我正在使用laravel用于我的web应用程序和routes.php我有:

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

I want to protect and check if the person is logged in , but this code always redirect to "/login" i want it to redirect to "admin/login" , can this be done? 我想保护并检查此人是否已登录,但此代码始终重定向到“/ login”我希望它重定向到“admin / login”,这可以完成吗?

There is a default auth filter in the filters.php file, it should be like this: filters.php文件中有一个默认的auth过滤器,它应该是这样的:

Route::filter('auth', function($route, $request)
{
    if (Auth::guest()) return Redirect::guest('login'); // /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. 此过滤器(如上所述)将检查用户是否未登录,然后将发生重定向并且用户将被发送到/login url ,否则将不会发生任何事情,用户将被发送到所请求的页面。

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: 此外,默认情况下,以下filter可用,此filter仅检查用户是否已登录,然后默认情况下将重定向到/ (主页):

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

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: 此( guest )过滤器与/login一起使用,如下所示,因此如果登录用户打算登录,则默认情况下用户将被重定向到主页:

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

Now, in your routes.php file you have following route declared: 现在,在routes.php文件中,您声明了以下路由:

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

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. 如果注销用户尝试访问admin则用户将被发送到login ,默认情况下这些用户可以使用。

Yes. 是。 Add the following filter: 添加以下过滤器:

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

I copied original auth filter delivered with Laravel. 我复制了与Laravel一起提供的原始auth过滤器。

Next, use your new filter: 接下来,使用您的新过滤器:

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

Complete solution: 完整解决方案

Route::filter('auth.admin', function() {
    // if not logged in redirect to the login page
    if (Auth::guest()) return Redirect::guest('admin/login');
});
Route::filter('auth.login', function() {
    // if already logged in don't show login page again
    if (Auth::check()) return Redirect::to('admin');
});

Route::get('admin/login', ['before' => 'auth.login', 'uses' => 'UsersController@getLogin']);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM