-->

Silex set cookie

2019-05-05 03:34发布

问题:

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));


标签: cookies silex