简体   繁体   English

Laravel 4登录重定向

[英]Laravel 4 login redirect

I do not want to show login page after login in laravel 4. If a logged in user want to visit login page it should redirect to homepage('/'). 在laravel 4中登录后,我不想显示登录页面。如果登录的用户想要访问登录页面,则应重定向到homepage('/')。 I am using Sentry for authentication. 我正在使用Sentry进行身份验证。

filter.php filter.php

Route::filter(
    'auth', function () {
    if (!Sentry::check()) {
        return Redirect::to('login');
    }
}

routes.php routes.php

Route::get('login', array('as' => 'login', function() {
return View::make('login');
}))->before('guest');

Route::post('login', 'AuthController@postLogin');

AuthController.php AuthController.php

function postLogin() {
    try {
        // Set login credentials
        $credentials = array(
            'email' => Input::get('email'), 'password' => Input::get('password')
        );

        // Try to authenticate the user
        $user = Sentry::authenticate($credentials, false);
        if ($user) {
            return Redirect::to('/');
        }
    } catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
        return Redirect::to('login')->withErrors('Login field is required');
    }
}

After successful login if, if login page is requested it still shows the login page 成功登录后,如果仍要求登录页面,则仍显示登录页面

If you're using Laravel's default guest filter, it will not work, because the default guest filter does not check if your Sentry user is logged in. 如果您使用的是Laravel的默认guest过滤器,它将无法使用,因为默认guest过滤器不会检查您的Sentry用户是否已登录。

Try this instead: 尝试以下方法:

Route::filter(
    'filter', function () {
    if (Sentry::check()) {
        return Redirect::to('/');
    }
}

In your routes.php, the filter is already being applied to the login route, so things should work this way. 在您的routes.php中,过滤器已经被应用到登录路由,因此事情应该以这种方式进行。

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

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