我尝试运行以下代码:
Session::put('progress', '5%');
dd(Session::get('progress'));
这将在转储中显示5%".
This will show '5%' in the dump.
如果我重新运行同一页面但注释掉 Session::put('progress', '5%');
以便只有 dd()
行调用时,我得到一个空值,而不是上一页加载中存储的 5% 值.
If I rerun the same page but comment out Session::put('progress', '5%');
so that only the dd()
line is called, I get a null value instead of the 5% values stored in the previous page load.
这是我的会话配置,所以我知道它应该存储数据:
Here is my sessions config, so I know it should be storing the data:
'driver' => 'native',
'lifetime' => 120,
'expire_on_close' => false,
为什么 Laravel 不跨页面加载存储会话数据?
Why is Laravel not storing the session data across page loads?
问题是因为你在 Laravel 完成其应用程序生命周期之前杀死了脚本,所以放入会话中的值(但尚未存储)也被杀死了.
The problem is because you are killing the script before Laravel finishes its application lifecycle, so the values put in session (but not yet stored) got killed too.
当 Laravel 应用程序生命周期开始时,Session
中的任何值 put
都尚未存储,直到应用程序生命周期结束.这就是 Session
中的任何值 put
最终/真正 stored
.
When a Laravel application lifecycle starts, any value put
in Session
are not yet stored until the application lifecycle ends. That is when any value put
in Session
will be finally/really stored
.
如果您查看来源,您会发现相同的上述行为:
If you check the source you will find the same aforementioned behavior:
public function put($key, $value)
{
$all = $this->all();
array_set($all, $key, $value);
$this->replace($all);
}
<小时>
如果要测试它,请执行以下操作:
If you want to test it, do the following:
在会话中存储一个值而不杀死脚本.
Store a value in session without killing the script.
Route::get('test', function() {
Session::put('progress', '5%');
// dd(Session::get('progress'));
});
检索已经存储的值:
Route::get('test', function() {
// Session::put('progress', '5%');
dd(Session::get('progress'));
});
这篇关于Laravel 会话数据不会跨页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!