普通网友 2013-11-16 03:37
浏览 7
已采纳

too long

i'm working with symfony and trying to set and get session variable to pass trough a twig template.

I tryied the way is in the symfony example: http://symfony.com/doc/current/components/http_foundation/sessions.html

$session = new Session();
$session->start();

// set and get session attributes
$session->set('name', 'Drak');
$session->get('name');

Twig:

{{ app.session.get('balance') }}

Returns this error message:

An exception has been thrown during the rendering of a template ("Failed to start the session: already started by PHP ($_SESSION is set).")

I check my php.ini and the sessions doesn't start automatycally, also there is no other session live.

I tried this way too:

   $session = $this->getRequest()->getSession();
// We store an attribute
        $session->set('FrenchTouch', 'Julian');
// in another controller for another request, we retrieve the value of this attribute
        $value = $session->get('FrenchTouch');
        print_r(array($value));

But returns this other error:

FatalErrorException: Error: Call to a member function get() on a non-object

  • 写回答

2条回答 默认 最新

  • doubipeng1336 2013-11-16 14:44
    关注

    I did find the answer,

    The problem wasn't in the way to get/set the session, the issue comes because i'm passing the info trough controllers and i was using a non-native symfony way. The correct way to pass info in controllers is:

    $response = $this->forward('YourBundle:YourController:YourAction', array(
     'user'=>$user
    ));
    return $response;
    

    And then in the other controller set the sessions and render the template.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题