How to access the session variable in the controller
On a more general note, if your controller extends from the base Symfony controller (Symfony\Bundle\FrameworkBundle\Controller\Controller
) you can get the session in 3 ways:
$session = $this->container->get('session');
$session = $this->get('session');
(which basically is a shortcut to 1)$session = $request->getSession();
While Cyprian answer is valid, you will find in the documentation the following usage:
use Symfony\Component\HttpFoundation\Session\Session;$session = new Session();$session->start();// set and get session attributes$session->set('id',$id);$session->get('id'); //this is the line you are looking for
http://symfony.com/doc/master/components/http_foundation/sessions.html
Note:
While it is recommended to explicitly start a session, a sessions will actually start on demand, that is, if any session request is made to read/write session data.