Here is an excerpt from one of my sites that sets a cookie, then serves as a pdf:
$dt = new \DateTime(); $dt->modify("+1 year"); $c = new Cookie("juniorkupon_letoltve", "1", $dt); $r = new Response(file_get_contents(ROOT . "/data/kupon.pdf"), 200, array("Content-Type" => "application/pdf")); $r->headers->setCookie($c); return $r;
The trick is that you need to create a Response object manually and set a cookie for it. You can configure the response to the output of twig rendering as follows:
$r = new Response($app["twig"]->render("filename", $params));
source share