Laravel不会删除/更新会话

时间:2015-02-03 03:52:06

标签: php session laravel laravel-4

我在会话变量中存储一个数组,我有一个从数组中删除项目的函数,我需要更新会话但是当我尝试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); 
}

还尝试了forget()然后set()由上面的函数调用:

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);
}

1 个答案:

答案 0 :(得分:0)

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

相关问题