Louis B
Louis B

Reputation: 306

Get userId on database in Silex Project

I started a new project with Silex, it looks like a cool and fast Framework but I'm getting crazy with the SecurityServiceProvider trying to get the uid. I though it should be something like the getId() like Symfony but I got an error that the function doesn't exists.

PHP Fatal error: Call to undefined method Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage::getUser()

If I got the current logged user I have his information but not the Id.

$user = $app['user'];
print_r($user);

Symfony\Component\Security\Core\User\User Object ( [username:Symfony\Component\Security\Core\User\User:private] => [email protected] [password:Symfony\Component\Security\Core\User\User:private] => 5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg== [enabled:Symfony\Component\Security\Core\User\User:private] => 1 [accountNonExpired:Symfony\Component\Security\Core\User\User:private] => 1 [credentialsNonExpired:Symfony\Component\Security\Core\User\User:private] => 1 [accountNonLocked:Symfony\Component\Security\Core\User\User:private] => 1 [roles:Symfony\Component\Security\Core\User\User:private] => Array ( [0] => ROLE_USER ) )

Any idea? Thanks in advance!

Upvotes: 0

Views: 399

Answers (2)

Louis B
Louis B

Reputation: 306

Ok got the solution, if anybody implements a new UserProvider, should to add the customs fields on the provider, and define them on User class in Symfony. Thanks anyway!

Upvotes: 0

Mert Öksüz
Mert Öksüz

Reputation: 1071

// Symfony 2.6+
$token = $app['security.token_storage']->getToken();

// Symfony 2.3/2.5
$token = $app['security']->getToken();

Get user info:

$user = $token->getUser();

Check $user

var_dump($user);

Upvotes: 0

Related Questions