【问题标题】:Argument 2 passed to Symfony\Component\HttpFoundation\Cookie::__construct() must be of the type string or null, array given传递给 Symfony\Component\HttpFoundation\Cookie::__construct() 的参数 2 必须是字符串类型或 null,给定数组
【发布时间】:2018-10-23 22:34:18
【问题描述】:

我已将我的 laravel 版本从 5.5 更新到 5.6,并按照更新指南进行操作,但有一个错误我无法弄清楚。

错误

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

我的代码产生了这个错误:

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 队列方法在尝试创建 Cookie 实例时会引发异常,但此代码过去在 5.5 中可以完美运行,并且 CookieJar 文档还建议它接受数组作为参数。

【问题讨论】:

  • 我看不到您在这段代码中的哪个位置实例化了 Cookie 对象。 PHP 错误会给你一个行号,Laravel 会给你一个回溯。该错误是不言自明的,您正在将一个数组传递到预期之外的地方。
  • 你的路线是什么样的?
  • cookie jar 对象尝试创建一个 Cookie 对象,该对象的值应该是字符串而不是数组,但这曾经可以工作...我知道,如果我将其更改为字符串,它将可以工作,但是文档表明它应该可以工作......@miken32

标签: php laravel laravel-5


【解决方案1】:

问题是在更新之后 Symphony 库也更新到了不再支持数组 cookie 的 3.3 版。此时,一个解决方案可能是序列化数组然后反序列化。

【讨论】:

    猜你喜欢
    • 2021-05-26
    • 2021-10-02
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多