简体   繁体   English

Laravel 4:页面刷新时不会消失会话Flash消息

[英]Laravel 4: Session Flash Message Not Disappearing on Page Refresh

I am using same method for get and post request of login authentication like 我使用相同的方法来获取和发布登录认证请求

public function login()
{
    if (Request::isMethod('post')){
        // Getting credentials
        $username = Input::get('username');
        $password = Input::get('password');
        // Authenticating super admin
        if(Auth::attempt(array('email'=>$username,'password'=>$password,'sysadmin'=>1))){

            return Redirect::To('superadmin');
        }else{
            Session::flash('message', 'Invalid Credentials !'); 
            return View::make('superadmin::login');         
        }           
    }else{
        echo View::make('superadmin::login');
    }
}

This is the code of login view to display error messages: 这是显示错误消息的登录视图代码:

@if(Session::has('message'))
    <div class="alert-box message">
        <h2>{{ Session::get('message') }}</h2>
    </div>
@endif

The problem is when i enter invalid credentials while login, then error message is displayed at the form but after that on manually refreshing the login page, session message does not disappear. 问题是当我在登录时输入无效凭据时,表单上会显示错误消息,但在手动刷新登录页面之后,会话消息不会消失。 Where as, on second time page refresh, session message disappears. 在第二次页面刷新时,会话消息消失。

I know that this is happening because URL (referrer) remain same in my case because same method "login" is being used for both types of request (get and post). 我知道这种情况正在发生,因为URL(referrer)在我的情况下保持相同,因为同样的方法“login”被用于两种类型的请求(get和post)。

What would be the best way to display error messages in this case so that on page refresh or automatically the message should disappear. 在这种情况下,显示错误消息的最佳方法是什么,以便在页面刷新或自动消息消失。

This is an old question but I want to explain the easiest way: 这是一个老问题,但我想解释最简单的方法:

@if(Session::has('message'))
    <div class="alert-box message">
        <h2>{{ Session::pull('message') }}</h2>
    </div>
@endif

Wouldn't this be a beter fit? 这不是一个合适的人吗?

return View::make('superadmin::login')->with('message', 'Invalid credentials');

and then in your view: 然后在你看来:

@if($message)
    <div class="alert-box message">
        <h2>{{ $message }}</h2>
    </div>
@endif

This is because you need not to flash, as you never use a redirect after a flash. 这是因为你不需要闪光,因为闪光后你永远不会使用重定向。

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

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