Session bag — Encapsulating dataset of attributes in Symfony 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;
<?php $session = new Session(); $session->start(); // setting and getting sessions $session->set('username', 'digvijaytiwari'); $session->get('name'); ?>
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.
Cheers!!!
Please follow and like us:
Read full story at www.spokenbyyou.com.