title: Symfony response->send() returns 500 error
body: <p>In symfony3 I'm trying to save a cookie. But when the cookie is made the first time, it returns a 500 error.</p>
  
  <p>This is the code to create the cookie:</p>
  
  <pre><code>protected function checkForCartId(Request $request)
  {
      if(!empty($this-&gt;uniqueCartId)) {
          return $this-&gt;uniqueCartId;
      }
  
      $response = new Response();
      if(!$request-&gt;cookies-&gt;has('uniqueCartId')) {
          $this-&gt;uniqueCartId = uniqid();
          $response-&gt;headers-&gt;setCookie(new Cookie('uniqueCartId', $this-&gt;uniqueCartId));
          $response-&gt;send();
  
          return $this-&gt;uniqueCartId;
      }
  
      return $request-&gt;cookies-&gt;get('uniqueCartId');
  }
  </code></pre>
  
  <p>When I remove the line <code>$response-&gt;send()</code> the error is gone, but the cookie is not saved.</p>
  
  <p>Any ideas? </p>
  
  <p>===========  <strong>EDIT</strong>  =============</p>
  
  <p>Log file (after getting error again)</p>
  
  <pre><code>[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 &gt; 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"} []
  </code></pre>
  
  <p>================= <strong>EDIT</strong> ===============
  Controller action:</p>
  
  <pre><code>public function getCartItems(Request $request)
  {
      $shoppingCartRepository = $this-&gt;em-&gt;getRepository(ShoppingCart::class);
  
      return $this-&gt;render('checkout/cart.html.twig', [
          'cartItems' =&gt; $shoppingCartRepository-&gt;findAllProductsInCart($this-&gt;getUser(), $this-&gt;checkForCartId($request))
      ]);
  }
  </code></pre>
  
  <p>================= <strong>EDIT</strong> ===============</p>
  
  <p>My New getCartItems funtion:</p>
  
  <pre><code>public function getCartItems(Request $request)
  {
      $shoppingCartRepository = $this-&gt;em-&gt;getRepository(ShoppingCart::class);
  
      if(!$request-&gt;cookies-&gt;has('uniqueCartId')) {
          $this-&gt;checkForCartId($request);
      }
  
      $response = $this-&gt;render('checkout/cart.html.twig', [
          'cartItems' =&gt; $shoppingCartRepository-&gt;findAllProductsInCart($this-&gt;getUser(), $this-&gt;checkForCartId($request))
      ]);
  
      $response-&gt;headers-&gt;setCookie(new Cookie('uniqueCartId', $this-&gt;checkForCartId($request)));
      return $response;
  }
  </code></pre>
  
  <p>============= <strong>SOLUTION</strong> =============</p>
  
  <pre><code>public function getCartItems(Request $request)
  {
      $shoppingCartRepository = $this-&gt;em-&gt;getRepository(ShoppingCart::class);
  
      $response = new Response();
      if(!$request-&gt;cookies-&gt;has('uniqueCartId')) {
          $response-&gt;headers-&gt;setCookie(new Cookie('uniqueCartId', $this-&gt;checkForCartId($request)));
          $response-&gt;sendHeaders();
      }
  
      return $this-&gt;render('checkout/cart.html.twig', [
          'cartItems' =&gt; $shoppingCartRepository-&gt;findAllProductsInCart($this-&gt;getUser(), $this-&gt;checkForCartId($request))
      ], $response);
  }
  </code></pre>