Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the session variable in the controller

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?

like image 804
bhari Avatar asked Jun 01 '13 12:06

bhari


People also ask

How do I access session variables?

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();

Which variable is used to access the session variables?

Session variables are set with the PHP global variable: $_SESSION.

Where Are session variables stored?

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).


2 Answers

There is session service which you should use:

$id = $this->get('session')->get('id');

or

$this->get('session')->set('id', $id);
like image 133
Cyprian Avatar answered Oct 20 '22 13:10

Cyprian


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:

  1. $session = $this->container->get('session');
  2. $session = $this->get('session'); (which basically is a shortcut to 1)
  3. $session = $request->getSession();
like image 25
Adrian C. Avatar answered Oct 20 '22 14:10

Adrian C.