Monday, February 1, 2010

Session in Symfony

Hi,
setting of session in symfony --
in action.class.php
session object for the current user is accessed in the action with the getUser() method and is an instance of the sfUser class.
This class contains a parameter holder that allows you to store any user attribute in it.
Ex:
class myModule extends sfActions
{
   public function executeFirst()
   {
    $name = $this->getRequestParameter('name');
    $this->getUser()->setAttribute('name', $name);
   }
   public function executeSecond()
   {
    $ses_name = $this->getUser()->getAttribute('name'); //Retrieve session values
   }
}

attributes are stored in a parameter holder that can be accessed by the getAttributeHolder() method. It allows for easy clean-up of the user attributes with the usual parameter holder methods.

//To remove session variable
$this->getUser()->getAttributeHolder()->remove('name');

//To clear session varaible
$this->getUser()->getAttributeHolder()->clear();

In Template:
The user session attributes are also available in the templates by default via the $sf_user variable, which stores the current sfUser object.

Ex:
$sf_user->getAttribute('name');

No comments :