Silex set cookie

断了今生、忘了曾经 提交于 2019-12-07 09:33:35

问题


I'm using Silex Framework, and I'm desperately trying to set a cookie. There's no information to be found in the docs, and I have tried almost anything!

Does someone possible have experience with this, and can provide a small example?

Thanks


回答1:


Here's an excerpt from one of my sites that sets a cookie then serves 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 you need to create the Response object manually and set the cookie on that. You can set the response to a twig render output like this:

$r = new Response($app["twig"]->render("filename", $params));


来源:https://stackoverflow.com/questions/13021440/silex-set-cookie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!