Reputation: 7895
The question is clear. as laravel documentation stated :
For example, the "session" middleware included with Laravel writes the session data to storage after the response has been sent to the browser
I guess it is because of response time saving but im not sure.
Upvotes: 2
Views: 614
Reputation: 19275
Checking the handle
method of the StartSession
middleware you can see that, when using the 'cookie' driver for the session storage, the data is written to the storage ( the cookie ) BEFORE the response is sent to the browser.
However, as you've said, when using, for example, 'file' or 'database' session driver, the data is written to the storage AFTER the response has been sent to the browser, in the terminate
method.
That happens probably for performance reasons: the primary concern of the app is to send the response to the client as soon as possible. If we're using the cookie driver, Laravel is someway 'forced' to store the data to the storage before sending the response, as the session info is needed to be stored in the cookie that is sent with the response. While, in the other cases, laravel can delay the writing to the storage, sending the response as soon as possible
Upvotes: 1
Reputation: 33048
That is the only reason I could think of why it would work like this.
If you need to push something to the session and have it immediately be available from the session, you can use the following...
Session::now($key, $value);
Upvotes: 1