Silex设置cookie [英] Silex set cookie

查看:183
本文介绍了Silex设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Silex框架,我拼命地尝试设置一个cookie。

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?

感谢

推荐答案

以下是我的一个网站的摘录然后提供pdf:

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;

诀窍是你需要创建 Response 对象手动并设置cookie。您可以将响应设置为twig渲染输出,如下所示:

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

这篇关于Silex设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆