I have created a session variable in one controller and I want to access it in another controller. In loginsuccess
controller I set the session:
$session->set('id',$id);
How can I access this variable in another controller?
You can access a session variable's value by using the global variable $_SESSION. In the example stated below, you will create another session with a variable that stores your name. session_start();
Session variables are set with the PHP global variable: $_SESSION.
By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier).
There is session
service which you should use:
$id = $this->get('session')->get('id');
or
$this->get('session')->set('id', $id);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With