Umair Malik
Umair Malik

Reputation: 1451

How to destroy session in symfony 2.6?

I tried doing this:

$session->remove();

Also this:

$session->clear();

But it gives following error:

Call to a member function clear() on a non-object

Upvotes: 3

Views: 9296

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

invalidate()

Clears all session data and regenerates session ID. Do not use session_destroy().

This actually does what you want.

Source & more info: http://symfony.com/doc/current/components/http_foundation/sessions.html

Also such things are easy to check by simply review the source.

For this case you should check Session or SessionInterface source:

http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Session/SessionInterface.html

http://api.symfony.com/2.6/Symfony/Component/HttpFoundation/Session/Session.html

Edit.

Of course this method belongs to Session class so you have to access Session object first in your controller.

So we go to:

http://symfony.com/doc/current/book/controller.html#managing-the-session

and we see how to do that:

public function indexAction(Request $request)
{
    $session = $request->getSession();

    $session->invalidate(); //here we can now clear the session.
}

Upvotes: 4

Related Questions