在symfony3中,我尝试保存cookie。但是当第一次创建cookie时,它会返回500错误。
这是创建cookie的代码:
protected function checkForCartId(Request $request)
{
if(!empty($this->uniqueCartId)) {
return $this->uniqueCartId;
}
$response = new Response();
if(!$request->cookies->has('uniqueCartId')) {
$this->uniqueCartId = uniqid();
$response->headers->setCookie(new Cookie('uniqueCartId', $this->uniqueCartId));
$response->send();
return $this->uniqueCartId;
}
return $request->cookies->get('uniqueCartId');
}
当我删除行$response->send()
时,错误消失了,但cookie未保存。
有什么想法吗?
=========== 编辑 =============
日志文件(再次收到错误后)
[2018-04-22 14:47:26] request.INFO: Matched route "homepage". {"route":"homepage","route_parameters":{"_controller":"AppBundle\\Controller\\DefaultController::indexAction","_route":"homepage"},"request_uri":"http://127.0.0.1:8000/","method":"GET"} []
[2018-04-22 14:47:26] security.DEBUG: Checking for guard authentication credentials. {"firewall_key":"main","authenticators":1} []
[2018-04-22 14:47:26] security.DEBUG: Calling getCredentials() on guard configurator. {"firewall_key":"main","authenticator":"AppBundle\\Security\\LoginFormAuthenticator"} []
[2018-04-22 14:47:26] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2018-04-22 14:47:26] doctrine.DEBUG: SELECT t0.id AS id_1, t0.url AS url_2, t0.title AS title_3, t0.content AS content_4, t0.seo_title AS seo_title_5, t0.seo_description AS seo_description_6, t0.published AS published_7 FROM pages t0 WHERE t0.url = ? LIMIT 1 ["/"] []
[2018-04-22 14:47:26] doctrine.DEBUG: SELECT p0_.id AS id_0, p0_.slug AS slug_1, p0_.name AS name_2, p0_.is_published AS is_published_3, p0_.description AS description_4, p0_.price AS price_5, p0_.discount_price AS discount_price_6, p0_.page_title AS page_title_7, p0_.seo_title AS seo_title_8, p0_.seo_description AS seo_description_9 FROM product p0_ WHERE p0_.discount_price > 0 AND p0_.is_published = ? [1] []
[2018-04-22 14:47:26] doctrine.DEBUG: SELECT h0_.id AS id_0, h0_.title AS title_1, h0_.subtitle AS subtitle_2, h0_.image AS image_3, h0_.button_text AS button_text_4, h0_.button_link AS button_link_5, h0_.active AS active_6 FROM homepage_banner h0_ WHERE h0_.active = ? [1] []
[2018-04-22 14:47:26] doctrine.DEBUG: SELECT c0_.id AS id_0, c0_.name AS name_1, c0_.slug AS slug_2, c0_.page_title AS page_title_3, c0_.description AS description_4, c0_.parent_id AS parent_id_5, c0_.created_by AS created_by_6 FROM category c0_ WHERE c0_.parent_id IS NULL [] []
[2018-04-22 14:47:26] doctrine.DEBUG: SELECT t0.id AS id_1, t0.name AS name_2, t0.slug AS slug_3, t0.page_title AS page_title_4, t0.description AS description_5, t0.parent_id AS parent_id_6, t0.created_by AS created_by_7 FROM category t0 WHERE t0.parent_id = ? [1] []
[2018-04-22 14:47:26] doctrine.DEBUG: SELECT t0.id AS id_1, t0.name AS name_2, t0.slug AS slug_3, t0.page_title AS page_title_4, t0.description AS description_5, t0.parent_id AS parent_id_6, t0.created_by AS created_by_7 FROM category t0 WHERE t0.parent_id = ? [2] []
[2018-04-22 14:47:26] doctrine.DEBUG: SELECT s0_.id AS id_0, s0_.cart_id AS cart_id_1, s0_.quantity AS quantity_2, s0_.price AS price_3, s0_.discount_price AS discount_price_4, s0_.ordered AS ordered_5, s0_.created_at AS created_at_6, s0_.product_id AS product_id_7, s0_.user_id AS user_id_8 FROM shopping_cart s0_ WHERE s0_.cart_id = ? ["5adc845e728a9"] []
[2018-04-22 14:47:26] request.CRITICAL: Uncaught PHP Exception UnexpectedValueException: "The Response content must be a string or object implementing __toString(), "boolean" given." at H:\Websites\CMS\vendor\symfony\symfony\src\Symfony\Component\HttpFoundation\Response.php line 406 {"exception":"[object] (UnexpectedValueException(code: 0): The Response content must be a string or object implementing __toString(), \"boolean\" given. at H:\\Websites\\CMS\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpFoundation\\Response.php:406)"} []
[2018-04-22 14:47:27] request.INFO: Matched route "_wdt". {"route":"_wdt","route_parameters":{"_controller":"web_profiler.controller.profiler:toolbarAction","token":"49bddb","_route":"_wdt"},"request_uri":"http://127.0.0.1:8000/_wdt/49bddb","method":"GET"} []
================= 编辑 =============== 控制器动作:
public function getCartItems(Request $request)
{
$shoppingCartRepository = $this->em->getRepository(ShoppingCart::class);
return $this->render('checkout/cart.html.twig', [
'cartItems' => $shoppingCartRepository->findAllProductsInCart($this->getUser(), $this->checkForCartId($request))
]);
}
================= 编辑 ===============
我的新getCartItems功能:
public function getCartItems(Request $request)
{
$shoppingCartRepository = $this->em->getRepository(ShoppingCart::class);
if(!$request->cookies->has('uniqueCartId')) {
$this->checkForCartId($request);
}
$response = $this->render('checkout/cart.html.twig', [
'cartItems' => $shoppingCartRepository->findAllProductsInCart($this->getUser(), $this->checkForCartId($request))
]);
$response->headers->setCookie(new Cookie('uniqueCartId', $this->checkForCartId($request)));
return $response;
}
============= 解决方案 =============
public function getCartItems(Request $request)
{
$shoppingCartRepository = $this->em->getRepository(ShoppingCart::class);
$response = new Response();
if(!$request->cookies->has('uniqueCartId')) {
$response->headers->setCookie(new Cookie('uniqueCartId', $this->checkForCartId($request)));
$response->sendHeaders();
}
return $this->render('checkout/cart.html.twig', [
'cartItems' => $shoppingCartRepository->findAllProductsInCart($this->getUser(), $this->checkForCartId($request))
], $response);
}
答案 0 :(得分:2)
您需要在Response对象上设置cookie,在您的情况下,它是渲染的树枝模板。经过一些重构后,您的代码可能如下所示:
protected function getCardId(Request $request)
{
if(!$request->cookies->has('uniqueCartId')) {
$this->uniqueCartId = uniqid();
return ['is_new' => true, 'unique_id' => $this->uniqueCartId];
}
$this->uniqueId = $request->cookies->get('uniqueCartId');
return ['is_new' => false, 'unique_id' => $request->cookies->get('uniqueCartId')];
}
...
public function getCartItems(Request $request)
{
$shoppingCartRepository = $this->em->getRepository(ShoppingCart::class);
$cartId = $this->getCardId($request);
$response = $this->render('checkout/cart.html.twig', [
'cartItems' => $shoppingCartRepository->findAllProductsInCart($this->getUser(), $cartId['uniqueId'])
]);
if($cartId['is_new']) {
$response->headers->setCookie(new Cookie('uniqueCartId', $this->uniqueCartId));
}
return $response;
}
需要注意的重要一点是,响应只返回一次,您无法为单个请求返回两个响应。