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?

  • 写回答

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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line