• 7

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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.

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


    class: MyApp\Service
    arguments: ['@session']


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

  • 57
Reply Report
      • 2
    • Hello! I'm new in SF2 and I'm trying to figure out how the sessions works.. could you tell me how can I set or get the sessions in the controller using this service please?
      • 2
    • Hi, from the controller you can access the entire 'service container'. So to get the session, you can do $session = $this->container->get('session'); Likewise any other service you need you can get the same way.
    • Starting with Symfony when all you have ever written is raw procedural PHP is tricky. I highly recommend you find a good tutorial of building a basic app start to finish and work through that, it will give you a much better idea of what things do and how to structure your app.
    • Great, I'm a Laravel developer and when start using symfony two months ago, was very different.. more "complicated" but thanks for the advice!
    • This is the better way to do what the OP asked to do. Instead of directly trying to access the session object from the service, it's better to inject it into the service. This is how I do it.

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

  • 28
Reply Report
    • Hi Problematic, thanks for your quick reply. I tried the $session = $this->container->get('session'); and although it worked it doesn't seem to save anything to the session. Do I need to do anything to pass the updated session data back?
      • 2
    • So, to set the session is $session = $this->getRequest()->getSession(); $session->set('foo', 'bar'); $session->save(); and to retrieve the session in another controller is $session = $this->container->get('session'); ?

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;
  • 2
Reply Report

faz assim que vai funcionar

use Symfony\Component\HttpFoundation\Session\Session;

class ClassType extends AbstractType {
    private $session;

     * @param FormBuilderInterface $builder
     * @param array $options
    public function buildForm(FormBuilderInterface $builder, array $options) {

        $this->session = new Session();

    ->add('field', $this->session->get('nameFieldSession'));


  • -2
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions