简体   繁体   中英

Argument 2 passed to Symfony\Component\HttpFoundation\Cookie::__construct() must be of the type string or null, array given

I have updated my laravel version from 5.5 to 5.6 and followed the update guide but there is one error that I cannot figure out.

Error

Argument 2 passed to Symfony\Component\HttpFoundation\Cookie::__construct() must be of the type string or null, array given

My code generating this error:

public function show($id, DealService $dealService, CookieJar $cookieJar)
    {
        if(null != $coupon = $dealService->getActiveDealById($id))
        {
            if(!Cookie::has('recent'))
            {
                $ids = [];
                array_unshift($ids, $id);
                $cookieJar->queue('recent', $ids);
            }
            else
            {
                $ids = Cookie::get('recent');
                if(!in_array($id, $ids))
                {
                    array_unshift($ids, $id);
                    $ids[] = $id;
                    $cookieJar->queue('recent', $ids);
                }
            }
            if(!empty($ids))
            {
                $recent_deals = $dealService->getDealsByIds($ids);
            }
            $related_deals = $dealService->getRelatedActiveDeals($id);

            return view('couponia.show', ['coupon' => $coupon, 'recent_deals' => $recent_deals, 'related_deals' => $related_deals]);
        }
        else
        {
            return view('errors.404');
        }
    }

CookieJar queue method throws the exception as it is trying to create a Cookie instance, but this code used to work perfectly in 5.5 and the CookieJar documentation also suggests that it accepts array as argument.

The Problem was that after the update the Symphony Library was also updated to version 3.3 which does not support array cookies anymore. At this point a solution could be to serialize the array and then unserialize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM