dsvtnz6350 2018-10-23 22:34
浏览 549
已采纳

参数2传递给Symfony \ Component \ HttpFoundation \ Cookie :: __ construct()必须是string或null类型,给出数组

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.

  • 写回答

1条回答 默认 最新

  • duanfuxing2212 2018-10-27 10:31
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题