dimanche 6 août 2017

Update a cookie holding an object with array doesn't work

I'm using a cookie to store a shopping basket. The basket holds:

  • An array containing the chosen items (represented as objects)
  • An promotion code (also represented as an object)

All the requests are made asynchronous with JS.

When the user chooses his items for the first time and goes to the overview, everything works perfect. When the user afterwards goes back to change his shopping basket (including the promotion code), the array is not changing, but the promotion code did. The changes seem to get lost between the first and second request (see below).

Code:

First request: selecting the items

$cookieObject = new VisitorCookieObject($items, $eventUrl);<- wrapper object for the basket
        return response()
            ->json(['saved' => true])
            ->withCookie(Cookie::make('order', $cookieObject, 60));

Second request: the overview

$cookieObject = $request->cookie('order');            
$cookieObject->createPromotionFromInput($request->all());
return response()
    ->json($cookieObject)
    ->withCookie(Cookie::make('order', $cookieObject, 60));

To allow the VisitorCookieObject to be stored in the cookie, I use json_encode. Although the VisitorCookieObject is recreated after the user chooses his items, the array seems to survive.

What am I doing wrong?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2hzBY11
via IFTTT

Aucun commentaire:

Enregistrer un commentaire