Reputation: 4568
Im making a deeply nested and reactive form renderer with Livewire. To do that I reached for a global store pattern, so any component in the hierarchy can refer to methods getStore()
and setStore($store)
.
It seem to work, I could implement the store getter/setter methods with the data residing either in a json file or by using the Cache facade. However, my first choice would be to use the session, something like:
public function getStore()
{
return Session::get($this->store_id);
}
public function setStore($store)
{
Session::put($this->store_id, $store);
}
But, I can't get this to work specifically with Session. Have a hard time debugging why that is. It seems sometimes a session key is not set when it should, even though logging indicating it was set without any errors. I also tried explicitly persisting it with Session::save();
in case it does not reach the Terminable Middleware where it is normally persisted. No effect.
Any ideas why my store setup works with file_get_contents/Cache but not with Session?
Upvotes: 1
Views: 730
Reputation: 21
Same happened to me. After refreshing the page, session would appear with old data. I even tried using setter with Session::forget()
function, but it didn't work.
private function set($cart)
{
Session::forget('cart');
Session::put('cart', $cart);
Session::save();
}
There was no error and logging data with Log::debug('after-set' . request()->session()->get('cart')['products'])
and Log::debug('after-page-refresh' . request()->session()->get('cart')['products'])
displayed 2 different arrays. Only thing that I found consistent is that after page refresh – only last Session::put()
request was not saved – all other changes before it took effect.
I'll probably have to use Cache like you did, because such inconsistency is to risky for production.
Upvotes: 2