【发布时间】: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