dtudj42064
2014-08-14 12:32
浏览 57
已采纳

如何在Symfony2中启用会话?

I would like to enable session in Symfony2, but I don't know how can I do that.

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. Do you have any ideas?

图片转代码服务由CSDN问答提供 功能建议

我想在Symfony2中启用会话,但我不知道该怎么做。

我将配置文件设置为:

  #app / config / config.yml 
framework:
 session:
 name:session  
 cookie_lifetime:0 
 cookie_httponly:true 
   
 
 

但似乎我的会话仍处于禁用状态且未启动。 我在我的控制器中测试了这段代码:

  echo(session_status()!== PHP_SESSION_ACTIVE)
?  “会话未启动!”
:“会话正常”; 
   
 
 

并返回“会话未启动!”。 它仅在我设置时才有效:

  if(session_status()== PHP_SESSION_NONE){
 session_start(); 
} 
    
 
 

但这是非常难看的解决方案,特别是我在Symfony2工作。 你有什么想法吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dsfdfd1211 2014-08-14 12:39
    已采纳

    You don't need to acitivate it, it's set per default with 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).

    15.04.2016 Edit:

    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

    app.request.session
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsfs504545 2014-08-14 12:38

    Sessions are automatically started when you read/write data in it if you use a recent version of 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.

    http://symfony.com/doc/current/components/http_foundation/sessions.html

    http://symfony.com/doc/current/components/http_foundation/session_php_bridge.html

    评论
    解决 无用
    打赏 举报
  • dongpao5658 2014-08-14 12:39

    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

    评论
    解决 无用
    打赏 举报
  • dongpan1871 2014-08-14 12:41

    In app/config/config.yml, under framework section, you must have a session item :

    framework:
        # ...
        session: ~
    

    Just have a look at : http://symfony.com/doc/current/reference/configuration/framework.html#session

    评论
    解决 无用
    打赏 举报
  • dpzzkfb1244 2014-08-14 12:46

    Syntax of Session in Symfony2 is bit different:

    $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');
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题