Question: 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?

Answer: 
There is `session` service which you should use:

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

```

or

```
$this->get('session')->set('id', $id);

```
**In a Symfony 6 controller:**

Add an argument to your action and type-hint it with `SessionInterface`. ([see doc](https://symfony.com/doc/current/controller.html#managing-the-session))

```
<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class FooController
{
    public function index(SessionInterface $session): Response
    {
        $session->set('id', $id);

        ...
    }
}

```

**In a Symfony 6 service:**

Inject the `RequestStack` service to the constructor and get the session from it. ([see doc](https://symfony.com/doc/current/session.html#basic-usage))

```
<?php

namespace App\Service;

use Symfony\Component\HttpFoundation\RequestStack;

class FooService
{
    public function __construct(private RequestStack $requestStack)
    {
    }

    public function bar()
    {
       $session = $this->requestStack->getSession();
       $session->set('id', $id);

       ...
    }
}

```