Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access a users session from a service in Symfony2?

Tags:

php

symfony

Does anyone know how to access the session variable in a service?

I have a service setup that I pass the container over to. I can access things like the Doctrine service by using:

// Get the doctrine service
$doctrine_service = $this->container->get('doctrine');  
// Get the entity manager
$em = $doctrine_service->getEntityManager();

However, I'm not sure how to access the session.

In a controller the session can be accessed using:

$session = $this->getRequest()->getSession();
$session->set('foo', 'bar');
$foo = $session->get('foo');

Is the session part of a service and if so what is the service called.

Any help would be really appreciated.

like image 299
Adam Stacey Avatar asked Aug 25 '11 15:08

Adam Stacey


People also ask

Where does Symfony store sessions?

However, if you have the following configuration, Symfony will store the session data in files in the cache directory %kernel. cache_dir%/sessions . This means that when you clear the cache, any current sessions will also be deleted: YAML.

What is session in Symfony?

Symfony provides a session object and several utilities that you can use to store information about the user between requests.


3 Answers

If you don't want to pass in the entire container you simply pass in @session in the arguments:

services.yml

my.service:
    class: MyApp\Service
    arguments: ['@session']

Service.php

use Symfony\Component\HttpFoundation\Session\Session;

class Service
{

    private $session;

    public function __construct(Session $session)
    {
        $this->session = $session;
    }

    public function someService()
    {
        $sessionVar = $this->session->get('session_var');
    }

}
like image 139
greg Avatar answered Oct 18 '22 10:10

greg


php app/console container:debug shows that the session service is a class instanceof Symfony\Component\HttpFoundation\Session, so you should be able to retrieve the session with that name:

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

After you've done your work on the session, call $session->save(); to write everything back to the session. Incidentally, there are two other session-related services that show in my container dump, as well:

session.storage instanceof Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage

session_listener instanceof Symfony\Bundle\FrameworkBundle\EventListener\SessionListener

like image 21
Derek Stobbe Avatar answered Oct 18 '22 09:10

Derek Stobbe


In Symfony 4, if you have your services configured to autowire you do not need to inject the session manually through services.yaml any longer.

Instead you would simply inject the session interface:

use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SessionService
{
    private $session;

    public function __construct(
        SessionInterface $session
    )
    {
        $this->session = $session;
    }
}
like image 3
Bananaapple Avatar answered Oct 18 '22 08:10

Bananaapple