【问题标题】:Livewire global store works with file and cache - but not with SessionLivewire 全局存储适用于文件和缓存 - 但不适用于 Session
【发布时间】:2021-04-06 05:58:53
【问题描述】:

我正在使用 Livewire 制作一个深度嵌套的反应式表单渲染器。为此,我采用了全局存储模式,因此层次结构中的任何组件都可以引用方法 getStore()setStore($store)

这似乎可行,我可以使用驻留在 json 文件中的数据或使用缓存外观来实现存储 getter/setter 方法。但是,我的第一选择是使用会话,例如:

public function getStore()

{
    return Session::get($this->store_id);
}

public function setStore($store)
{
    Session::put($this->store_id, $store);
}

但是,我不能让它专门用于 Session。很难调试为什么会这样。有时似乎没有设置会话密钥,即使记录表明它已设置且没有任何错误。我还尝试使用Session::save(); 显式持久化它,以防它无法到达通常持久化的可终止中间件。没有效果。

任何想法为什么我的商店设置适用于 file_get_contents/Cache 但不适用于 Session?

【问题讨论】:

    标签: php laravel laravel-livewire


    【解决方案1】:

    同样的事情发生在我身上。刷新页面后,会话将出现旧数据。我什至尝试使用带有Session::forget() 函数的setter,但它不起作用。

    private function set($cart)
        {
            Session::forget('cart');
            Session::put('cart', $cart);
            Session::save();
        }
    

    Log::debug('after-set' . request()->session()->get('cart')['products'])Log::debug('after-page-refresh' . request()->session()->get('cart')['products']) 没有错误,并且记录数据显示 2 个不同的数组。我发现唯一一致的是,在页面刷新之后——只有最后一个Session::put() 请求没有保存——所有其他更改在它生效之前。 我可能不得不像你一样使用缓存,因为这种不一致会给生产带来风险。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2010-11-12
      • 1970-01-01
      • 2021-01-06
      • 2017-01-04
      相关资源
      最近更新 更多