douhu8851 2019-04-22 16:18
浏览 53

Laravel 5.2会话闪存第一次不起作用

UPDATE: My issue had nothing to do with Laravel sessions, it was my Service Worker that was causing sessions to mess up! Although I still don't understand why the service worker was fine for a different hostname with the same code, but after disabling it sessions work fine now! Will leave this here in case anyone else stumbles upon session issues and don't immediately think about any service workers they have.


Since migrating to Laravel 5.2 I've been having session persistence problem with Session::flash and subsequent Session::keep usage.

I know I'm supposed to use the web middleware group, and I am. But that isn't solving my problem. Please see below:

app/Http/Kernel.php

protected $middlewareGroups = [
    'web' => [
        ...
        \Illuminate\Session\Middleware\StartSession::class,
    ]
];

app/Http/routes.php

Route::get('test', function(Request $request) {
    $request->session()->flash('test_flash', "Some value");
    return redirect('/flashed');
});
Route::get('flashed', function(Request $request) {
    $request->session()->reflash();
    return redirect('/flash-kept');
});
Route::get('flash-kept', function(Request $request) {
    var_dump($request->session()->get('test_flash'));
});

The first time I hit /test it redirects to /flash-kept with value of test_flash as NULL - but second time I hit it, again it redirects but this time with correct value! And this is consistent in that every time I load any other page, then load /test it doesn't keep the flash message, only second time hitting it does.

Can someone see anything wrong??

This is happening on a Debian machine running PHP 5.6.30, very interestingly I cannot replicate this behaviour on my local machine, macOS running PHP 7.1 - but I don't think it's PHP version either as I quickly tried downgrading my local machine to use PHP 5.6 and I still couldn't re-create the issue! I've been staring at this for the last few hours, cannot see anything wrong!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答