dsy6639 2016-08-24 07:09
浏览 173

Laravel5 Session :: flash()持续存在的请求多于一个

I'm solving quite strange problem:

I try to log in user, if credentials are incorrect, I flash message into Session and redirect user bach to login page. There is shown message, which was flashed into session. Anyway, after I do reload of the page, there is still message in the Session, however, it should disappear.

Could you provide me your help? Here is my code, where I flash message into session:

  public static function verify($email, $password) {

    $verified = Auth::user()->attempt([
        'email' => $email,
        'password' => $password
    ]);

    if($verified) {
            $user = Auth::user()->get();
            Session::flash('message', [
                'title' => trans('user.logged_in'.($user->sex != null ? '_'.$user->sex : '')),
                'status' => 'success'
            ]);
            return redirect(route('adminDashboard'));
        }
        else {
            Session::flash('message', [
                'title' => trans('user.bad_credentials'),
                'status' => 'error'
            ]);
            return redirect()->back();
        }

    return $verified;

}

Here is code of page, which is shown on address, where I redirect, after incorrect credentials (redirect()->back())

@if(Session::has('message'))
    <?php
    $message = Session::get('message');
    $status = isset($message['status']) ? $message['status'] : 'success';

    $messageString = '';
    if(isset($message['title']) && $message['title'] != '')
        $messageString = '"'.$message['title'].'"';

    if(isset($message['text']) && $message['text'] != '')
        $messageString .= ($messageString != '' ? ',' : '').'"'.$message['text'].'"';

    ?>
    <script>
        $(document).ready(function () {
            Notify({!! $messageString !!}).{{ $status }}();
        });
    </script>
@endif

I'm quite confused about this behaviour. I also tried to manually call Session::forget('message') in View, after I show the message, but message disappeared from session, if I immediately after this print Session::all(), but after I did reload, message was again in Session.

Thank you very much for your help.

  • 写回答

1条回答 默认 最新

  • dougang7521 2016-08-24 07:24
    关注

    Chang the following line:

    return redirect(route('adminDashboard'));
    

    to

    return redirect()->to('/adminDashboard');
    

    I am using the same in my project and it works fine.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?