Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

session Symfony2 not keep values

Tags:

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

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

like image 310
Munir Avatar asked Mar 24 '26 12:03

Munir


1 Answers

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);
    }
}
like image 66
Florent Avatar answered Mar 26 '26 01:03

Florent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!