Nadjib Mami
Nadjib Mami

Reputation: 5830

How to set and destroy sessions in PHP Symfony

How to set and destroy a session on the controller side?
Tried in the view side, works just fine. But now need it from inside the controller.

Upvotes: 4

Views: 9149

Answers (4)

notsure
notsure

Reputation: 381

UPDATE:

Destroy a Session in Symfony 2 as follows:

$request->getSession()->invalidate(1);

As invalidate leaves the current session untouched if you are not providing any parameter you have to set the lifetime on 1 (one second)

Symfony 3.4 documentation: http://api.symfony.com/3.4/Symfony/Component/HttpFoundation/Session/Session.html#method_invalidate

Upvotes: 3

Cristian Bujoreanu
Cristian Bujoreanu

Reputation: 1167

Did you try?

/** @var $session Session */
$session = $request->getSession();
$session->remove('name');

Upvotes: 1

Nadjib Mami
Nadjib Mami

Reputation: 5830

At last I found the solution just here around. Use the "session" service explained here: Old post

Upvotes: 0

Roshan Wijesena
Roshan Wijesena

Reputation: 3136

Did you try

$this->getAttributeHolder()->remove('foo');

if it was saved in namespace foobar

$user->getAttributeHolder()->remove('foobar','','foo');

Upvotes: 0

Related Questions