2012-08-23 9 views
3

使用Symfony2我想在用登錄後獲得的信息登錄後增加security.content用戶。設置Symfony2 security.context登錄後的用戶數據

所以在登錄成功的代碼我做AccountController如下:

$user = $this->get('security.context')->getToken()->getUser(); 

// Get everything we'll ever need for this user 
$user->fillDetails($this, $this->container); 

$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles()); 

// Give it to the security context 
$this->container->get('security.context')->setToken($token); 

return $this->redirect($this->generateUrl('AccountBundle_homepage')); 

如果我立即調用setToken()此信息在用戶對象設置fillDetails()後再次檢索用戶仍然存在。

但是在AccountBundle_homepage控制器動作,當我使用

$user = $this->get('security.context')->getToken()->getUser(); 

用戶我fillDetails()設置額外的信息不再出現,或0

任何幫助表示讚賞。

+0

不能確定這一點,但你可能需要將用戶堅持到數據庫(或任何用戶保存) –

+0

是的,@carlosGranados是正確的。在每個請求中,Symfony2在從會話中加載令牌後調用'$ userProvider-> refreshUser()'方法,通常從數據庫或定義的其他數據源加載用戶。 –

+0

謝謝大家,非常感謝。 –

回答