bhari
bhari

Reputation: 291

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?

Upvotes: 17

Views: 57768

Answers (7)

Murat Cakmak
Murat Cakmak

Reputation: 329

Symfony 6.2+
Manually access
Set

$session = new Session();
$session->set('db_user', $site->getUser());

Get

$session = new Session();
dd($session->all());

Or You can access with RequestStack

/** @var RequestStack $request */
$request = $this->container->get(RequestStack::class);
if ($request->getCurrentRequest()?->hasSession()) {
    $session = $request->getCurrentRequest()->getSession();
}

Upvotes: 0

Maxime W.
Maxime W.

Reputation: 1

For Symfony 6

Since Symfony 6 session is now available only through the Request object and the RequestStack see https://symfony.com/doc/current/session.html

So in your controller inject Request Object then from this request object you can now access the session with getSession method

//inside your controller

use Symfony\Component\HttpFoundation\Request;

public function index(Request $request):Response
{
    $session = $request->getSession();
}

Hope this helps ( this is my first answer ) ;)

Upvotes: 0

Baishu
Baishu

Reputation: 1591

In a Symfony 6 controller:

Add an argument to your action and type-hint it with SessionInterface. (see doc)

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

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

       ...
    }
}

Upvotes: 0

Vincent Decaux
Vincent Decaux

Reputation: 10714

There is a third way as writing in a comment :

use Symfony\Component\HttpFoundation\Session\SessionInterface;

public function indexAction(SessionInterface $session)
{
     $session->set('test', 'yes !');
}

This way allows you to get type-hinted variable, this way you can access methods of the Session object in your IDE.

Upvotes: 1

Adrian C.
Adrian C.

Reputation: 762

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

Upvotes: 17

cheesemacfly
cheesemacfly

Reputation: 11762

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.

Upvotes: 5

Cyprian
Cyprian

Reputation: 11374

There is session service which you should use:

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

or

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

Upvotes: 33

Related Questions