[英]How can I enable session in Symfony2?
I would like to enable session in Symfony2, but I don't know how can I do that. 我想在Symfony2中启用会话,但是我不知道该怎么做。
I set my config file like this: 我将配置文件设置如下:
#app/config/config.yml
framework:
session:
name: session
cookie_lifetime: 0
cookie_httponly: true
But it seems to my session is still disabled and not started. 但是,看来我的会话仍处于禁用状态且尚未启动。 I tested this code in my controller: 我在控制器中测试了以下代码:
echo ( session_status() !== PHP_SESSION_ACTIVE )
? "Session is not started!"
: "Session OK";
And it return "Session is not started!". 并返回“会话未开始!”。 It works only when I set: 它仅在我设置时有效:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
But this is very ugly solution, especially that I am working in Symfony2. 但这是非常丑陋的解决方案,尤其是我在Symfony2中工作。 Do you have any ideas? 你有什么想法?
You don't need to acitivate it, it's set per default with symfony. 您不需要激活它,它是使用symfony默认设置的。
The default entry is this: 默认条目是这样的:
framework:
session: ~
You also don't need to start a new session (in fact you'll receive an error because it's already started from symfony). 您也不需要启动新的会话(实际上,您会收到一条错误消息,因为它已经从symfony启动了)。
15.04.2016 Edit: 15.04.2016编辑:
The Syntax is a bit different by now 语法现在有点不同
Controller 控制者
$session = $this->get('session');
Twig 枝条
app.session
Old Version: 旧版本:
You just need to get the session in your controller from the Request! 您只需要从“请求”中获取控制器中的会话即可!
$session = $this->getRequest()->getSession();
That's all. 就这样。
In Twig you can access it via 在Twig中,您可以通过访问
app.request.session
In my current project I've enabled session like this and it works fine: 在我当前的项目中,我已经启用了这样的会话,并且可以正常工作:
(under framework option) (在框架选项下)
session:
handler_id: ~
cookie_domain: .st.dev
name: SFSESSID
cookie_lifetime: 0
save_path: "%kernel.cache_dir%/sessions"
gc_divisor: 2000
gc_maxlifetime: 86400
you can also look at this documenation page 您也可以查看此文档页面
In app/config/config.yml, under framework section, you must have a session item : 在app / config / config.yml的“框架”部分下,您必须具有一个会话项:
framework:
# ...
session: ~
Just have a look at : http://symfony.com/doc/current/reference/configuration/framework.html#session 看看: http : //symfony.com/doc/current/reference/configuration/framework.html#session
Syntax of Session in Symfony2 is bit different: Symfony2中Session的语法有点不同:
$session = $this->getRequest()->getSession();
// store an attribute for reuse during a later user request
$session->set('foo', 'bar');
// in another controller for another request
$foo = $session->get('foo');
Sessions are automatically started when you read/write data in it if you use a recent version of Symfony2. 如果使用最新版本的Symfony2,则当您在其中读取/写入数据时,会话会自动启动。
However, you can force it to start by instanciating a session variable and start it: 但是,您可以通过实例化会话变量并启动它来强制其启动:
$session = new Session();
$session->start();
You should take a look at the doc. 您应该看一下文档。 Be careful, Symfony sessions are not the same thing as native sessions. 请注意,Symfony会话与本机会话不是同一回事。
http://symfony.com/doc/current/components/http_foundation/sessions.html http://symfony.com/doc/current/components/http_foundation/sessions.html
http://symfony.com/doc/current/components/http_foundation/session_php_bridge.html http://symfony.com/doc/current/components/http_foundation/session_php_bridge.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.