session Symfony2不保留值

时间:2012-08-08 16:10:15

标签: symfony

class myController extends Controller
{
    public function newAction($id)
    {
        $session = $this->get('session');

        if(is_null(($session->get('foo')))){
            echo "the variable foo is no set in session";
            $session->set('foo', 'bar');
        }
    }
}

为什么每次加载动作时都会出现回音消息?

1 个答案:

答案 0 :(得分:1)

这对我有用:

class SessionTestController extends Controller
{
    public function testAction()
    {
        $session = $this->get('session');

        if (is_null($session->get('foo'))) {
            $session->set('foo', 'bar');
            $responseText = 'foo is null';
        } else {
            $responseText = 'foo is set';
        }

        // Use a response object
        return new Response($responseText);
    }
}