Session bag – Encapsulating dataset of attributes in Symfony 2

  • 2

PHP has its own session management system and it requires $_SESSION to access session variables. However, it interferes the code testability while working with OOPs in PHP. To overcome this, Symfony 2 introduces concept of session bag that is linked to encapsulating a specific dataset of attributes. Each of these bags stores all of it’s data under a unique namespace. This allow symfony to co-work with other applications that uses $_SESSION super-global and nall data remains completely with symfony’s session management.

Session management is handled by Symfony HttpFoundation component, that has a very powerful and flexible session subsystem which is designed to provide session management through a simple object-oriented interface using a variety of session storage drivers.

Symfony sessions are designed to replace PHP session functions like session_start(), session_destroy(), session_id() etc. Symfony sessions are incompatible with php.ini directive session.auto_start = 1 This directive should be turned off in php.ini, in the webserver directives or in .htaccess.


#Session WorkFlow


#Session Attributes

Session attributes are stored in a bag, and acts like an array.

  • set() : Sets an attribute by key.
  • get() : Gets an attribute by key.
  • all() : Gets all attributes as an array of key => value.
  • has() : Returns true if the attribute exists.
  • replace() : Sets multiple attributes at once: takes a keyed array and sets each key => value pair.
  • remove() : Deletes an attribute by key.
  • clear() : Clear all attributes.


#Bag Management

  • registerBag()  : Registers a SessionBagInterface.
  • getBag()  : Gets a SessionBagInterface by bag name.
  • getFlashBag() : Gets the FlashBagInterface. This is just a shortcut for convenience.


#Basic Example

use Symfony\Component\HttpFoundation\Session\Session;
$session = new Session();
// setting and getting sessions
$session->set('username', 'digvijaytiwari');

This is the small information about Symfony session. Try this Guys and write me up if you get any issue. If you have more information then please write a guest article here. I will publish that with your name.



  • I simply have to tell you that I like your blogs blog. Will you write more about this? You always know just what to say. I really like your write up. Colors seem brighter when you are around.

  • I shared your write up on Tumblr. I would like to be a mentor in this topic. I never discovered any attention-grabbing blogs like yours. Somehow you make time stop and fly at the same time. Do you have any video of that?

    • admin

      Thanks. No I don’t have any video. But planning to do it. BTW subscribe our desktop notification for stories on this site.

  • It is very informative and you are obviously very knowledgeable in this area. I hate that my computer battery is about dead. Without proper research, your readers will not be interested and you would lose credibility. Kudos.