Friday, 11 June 2010

How to set, get and remove session variables in symfony?

## Create Session Variables...
$this->getUser()->setAttribute('nickname', 'ashwani');


## Retrieve Session Variables...
$this->nickname = $this->getUser()->getAttribute('nickname');
   
## Check if session is exist return 1 else blank value...
$hasNickname = $this->getUser()->hasAttribute('nickname');

## Remove Session Variable...
$this->getUser()->getAttributeHolder()->remove('nickname');

Cheers!

4 comments:

  1. Retrieve Session Variables In template:
    $sf_user->getAttribute('id');
    In Model:
    sfContext::getInstance()->getUser()->getAttribute('id');

    ReplyDelete
  2. Hi.

    What about setting session variables in AppKernel.php.

    ReplyDelete
  3. how i verify if my sesions are started?

    ReplyDelete
  4. you can check the session is already started by
    $session = new Session();
    if($this->container->get('session')->isStarted())
    {
    if( $session->get('user') == "user" )

    return $this->render('CareerBundle:Backend:home.html.twig');
    else
    {
    return $this->render('CareerBundle:Backend:myhome.html.twig');
    }
    }
    else { return $this->redirectToRoute('career_backend_login_check'); }

    ReplyDelete