php Laravel 会话数据不会跨页面加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23917409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel session data not sticking across page loads
提问by eComEvo
I tried running the following code:
我尝试运行以下代码:
Session::put('progress', '5%');
dd(Session::get('progress'));
This will show '5%' in the dump.
这将在转储中显示“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.
如果我重新运行同一页面但注释掉Session::put('progress', '5%');
以便只dd()
调用该行,我将得到一个空值,而不是存储在前一个页面加载中的 5% 值。
Here is my sessions config, so I know it should be storing the data:
这是我的会话配置,所以我知道它应该存储数据:
'driver' => 'native',
'lifetime' => 120,
'expire_on_close' => false,
Why is Laravel not storing the session data across page loads?
为什么 Laravel 不跨页面加载存储会话数据?
回答by Rubens Mariuzzo
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 完成其应用程序生命周期之前杀死了脚本,因此放入会话(但尚未存储)的值也被杀死了。
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
.
当Laravel应用程序生命周期开始时,任何价值put
在Session
现时仍未保存,直到应用程序生命周期结束。也就是说,当任意值put
在Session
将最后/真的stored
。
If you check the sourceyou 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')); });
Retrieve the value already stored:
Route::get('test', function() { // Session::put('progress', '5%'); dd(Session::get('progress')); });
在会话中存储值而不终止脚本。
Route::get('test', function() { Session::put('progress', '5%'); // dd(Session::get('progress')); });
检索已经存储的值:
Route::get('test', function() { // Session::put('progress', '5%'); dd(Session::get('progress')); });
回答by sergei
回答by Alex
For me, even after data has been stored to session properly:
对我来说,即使在数据已正确存储到会话之后:
dd(Session::all());
returns nothing, but:
什么都不返回,但是:
print_r(Session::all());
returns all session data!
返回所有会话数据!
回答by Artur Owczarek
In my case I flashed the variable in one request and then put it into session in another request (with the same name).
在我的情况下,我在一个请求中刷新了变量,然后在另一个请求中将其放入会话(具有相同的名称)。
Unfortunatelly, terminating method went through all the previously flashed properties and cleaned my newly created session property (it was flushed in previous request so laravel thought it was no longer required and couldn't tell it was newly created). I figured it out debugging Kernel->terminateMiddleware
method. You can put a breakpoint in terminating method. At some stage it reaches Store->ageFlashData
. This is the method responsible for deleting my property.
不幸的是,终止方法遍历了所有以前刷新的属性并清除了我新创建的会话属性(它在之前的请求中被刷新,所以 laravel 认为它不再需要并且无法判断它是新创建的)。我想出了调试Kernel->terminateMiddleware
方法。您可以在终止方法中放置一个断点。在某个阶段它达到Store->ageFlashData
. 这是负责删除我的财产的方法。