【问题标题】:Laravel won't delete/update a sessionLaravel 不会删除/更新会话
【发布时间】:2015-04-02 04:28:05
【问题描述】:

我将一个数组存储在会话变量中,我有一个函数可以从数组中删除一个项目,我需要更新会话但是当我尝试 unset() 然后 Session::set() 它没有更新甚至调用 Session::flush() 都不会清除会话。我可以在会话中访问数组,但无法动态更新或删除它。

public static function removeFromCart($product_id)
{
    $key        =   'shopping_cart_items';
    $items      =   Session::get($key);
    $index      =   array_search($product_id, $items);
    unset($items[$index]);
    Session::set($key, $items); 
}

还尝试了由上述函数调用的忘记()然后设置():

public static function refreshCartSession($items)
{
    $key    =   'shopping_cart_items';
    Session::forget($key);
    Session::set($key, $items);
}

在我的控制器中,当我 dd() Session::get($key) 时,我可以看到特定项目已被删除,但是当我重新加载会话时,似乎回到了前一个数组并且我所做的更改没有效果:

    public function removeItemFromCart()
{
    $success_message    =   'Cart item has been removed.';
    $fail_message       =   'Failed to remove cart item.';
    $remove_key         =   'car_remove_item';
    if(Input::has($remove_key))
    {
        $item   =   Input::get($remove_key);
        ShopProducts::removeFromCart($item);
        return View::make('shop.shopcart')->with('cart_remove_success', dd(Session::get('shopping_cart_items')));      
    }
    else
        return View::make('shop.shopcart')->with('cart_remove_fail', $fail_message);
}

【问题讨论】:

  • 你为什么要用dd 杀死最后一个代码示例中的脚本执行?您需要正确返回视图,以便在框架运行其周期时正确编写会话。提前停止该循环将阻止会话被写入。
  • er 成功了。我只是 dd() 会话变量,以查看未设置是否已将其删除。我以为我在没有 dd() 的情况下进行了测试,但我猜不是。

标签: php session laravel laravel-4


【解决方案1】:

只有在整个请求周期发生时才会更新会话,因此如果您在执行期间dd('anything'),请求将保持不变。

【讨论】:

    猜你喜欢
    • 2019-12-31
    • 2018-10-20
    • 2015-04-02
    • 2016-05-27
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    相关资源
    最近更新 更多