问题已经很明确了。作为 laravel documentation声明:
For example, the "session" middleware included with Laravel writes the session data to storage after the response has been sent to the browser
我猜这是因为节省了响应时间,但我不确定。
最佳答案
检查StartSession
中间件的handle
方法可以看到,当使用'cookie'驱动程序进行 session 存储时,数据被写入存储( cookie ) 在响应发送到浏览器之前。
但是,正如您所说,当使用"file"或“数据库” session 驱动程序时,数据会在响应发送到浏览器后写入存储,在终止中
方法。
发生这种情况可能是出于性能原因:应用程序的主要关注点是尽快将响应发送给客户端。如果我们使用 cookie 驱动程序,Laravel 在发送响应之前会以某种方式“强制”将数据存储到存储中,因为 session 信息需要存储在与响应一起发送的 cookie 中。而在其他情况下,laravel 可以延迟写入存储,尽快发送响应
关于php - 为什么 Laravel 在响应发送到浏览器后存储 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38105946/