我开始使用Silex开发一个新项目,它看起来像一个很酷的快速框架,但我对于试图获取uid的SecurityServiceProvider感到疯狂。我虽然它应该像像Symfony这样的getId(),但我得到一个错误,该函数不存在。
PHP致命错误:调用未定义的方法Symfony \ Component \ Security \ Core \ Authentication \ Token \ Storage \ TokenStorage :: getUser()
如果我有当前登录的用户,我有他的信息但不是Id。
$user = $app['user'];
print_r($user);
Symfony \ Component \ Security \ Core \ User \ User Object( [username:Symfony \ Component \ Security \ Core \ User \ User:private] => myuser@mymail.com [密码:Symfony \ Component \ Security \ Core \ User \ User:private] => 5FZ2Z8QIkA7UTZ4BYkoC + GsReLf569mSKDsfods6LYQ8t + a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg == [enabled:Symfony \ Component \ Security \ Core \ User \ User:private] => 1 [accountNonExpired:Symfony的\分量\安全\核心\用户\用户:私人] => 1 [credentialsNonExpired:Symfony \ Component \ Security \ Core \ User \ User:private] => 1 [accountNonLocked:Symfony \ Component \ Security \ Core \ User \ User:private] => 1 [角色:Symfony \ Component \ Security \ Core \ User \ User:private] =>数组([0] => ROLE_USER))
有什么想法吗?提前谢谢!
答案 0 :(得分:0)
// Symfony 2.6+
$token = $app['security.token_storage']->getToken();
// Symfony 2.3/2.5
$token = $app['security']->getToken();
获取用户信息:
$user = $token->getUser();
检查$user
var_dump($user);
答案 1 :(得分:0)
好的解决方案,如果任何人实现了新的UserProvider,应该在提供者上添加海关字段,并在Symfony的User类中定义它们。不管怎样,谢谢!