2
votes

I am trying to setup a symfony session variable that I can access across all my actions/templates. This is done once the user credentials have been validated, the user authenticated but before he is redirected to his homepage.

The code looks like this:

// Authentication verified 

$this->getUser()->setAuthenticated(true);
// note I am deliberately using a static value
$this->getUser()->setAttribute('userid',"4"); 
$this->redirect('flashcard/index');  

When I attempt to get this attribute in one of my actions, like this:

$this->getUser()->getAttribute('userid') it returns null,

I take a var_dump like this:

var_dump($this->getUser()->getAttributeHolder()->getAll());

and it returns an empty array, which means something is not quite correct.

As a heads up, I am not using sfDoctrineGuard for login authentication, although I don't think setting session variables will have anything to do with it.

1
Is the user logged in when you call $this->getUser()->getAttribute('userid')?1ed

1 Answers

2
votes

Not sure what the error is, but here are a few suggestions to improve your code and try to debug it:

1 - Add custom functions for that in your user class so you'll call $this->getUser()->setMyVariable(4), $this->getUser()->getMyVariable(), $this->getUser()->isMyVariableDefined()... That would avoid any typo on the name and namespace...

2 - Add a namespace just in case (to avoid conflict on generic names such as userid)...

Something like that:

public function setMyVariable($value = null)
{
  $this->setAttribute('user_id', $value, 'some_namespace');
}

3 - Once the user is logged-in, check the user attributes using the debug bar - no need to code it. If it's there, keep checking to find out when it disappears...