3
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');
        }
    }
}

Why the msg of the echo appears every time the action is load?

3
  • 2
    Can you paste the result of var_dump($session->isStarted())? If false, try $session->start(). If true, check your browser settings! Commented Aug 8, 2012 at 16:26
  • I'm using Symfony 2.0 . This method is only for Symfony 2.1 . I put the $session->start() and check browser settings. In config.yml the session auto_start is true. The msg still appear. Commented Aug 9, 2012 at 11:46
  • The problem is I have another object(an entity) which is storing in session either. Don't know why it's interfering in others variables of session. When remove it everything works. Now I have another problem, store entity in session. But I will search more. Thx anyway! Commented Aug 9, 2012 at 12:46

1 Answer 1

1

This works for me:

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.