In symfony 4 (probably 3.3 also, but only real-tested in 4) you can inject the Security
service via auto-wiring in the controller like this:
<?php
use Symfony\Component\Security\Core\Security;
class SomeClass
{
/**
* @var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public function privatePage() : Response
{
$user = $this->security->getUser(); // null or UserInterface, if logged in
// ... do whatever you want with $user
}
}
As @ktolis says, you first have to configure your /app/config/security.yml
.
Then with
$user = $this->get('security.token_storage')->getToken()->getUser();
$user->getUsername();
should be enougth!
$user
is your User Object! You don't need to query it again.
Find out the way to set up your providers in security.yml
from Sf2 Documentation and try again.
Best luck!
According to the documentation since Symfony 2.1 simply use this shortcut :
$user = $this->getUser();
The above is still working on Symfony 3.2 and is a shortcut for this :
$user = $this->get('security.token_storage')->getToken()->getUser();
The
security.token_storage
service was introduced in Symfony 2.6. Prior to Symfony 2.6, you had to use thegetToken()
method of thesecurity.context
service.
Example : And if you want directly the username :
$username = $this->getUser()->getUsername();
The user will be an object and the class of that object will depend on your user provider.
The thread is a bit old but i think this could probably save someone's time ...
I ran into the same problem as the original question, that the type is showed as Symfony\Component\Security\Core\User\User
It eventually turned out that i was logged in using an in memory user
my security.yml looks something like this
security:
providers:
chain_provider:
chain:
providers: [in_memory, fos_userbundle]
fos_userbundle:
id: fos_user.user_manager
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN', 'ROLE_SONATA_ADMIN' ] }
the in_memory user type is always Symfony\Component\Security\Core\User\User if you want to use your own entity, log in using that provider's user.
Thanks, hj
In symfony >= 3.2, documentation states that:
An alternative way to get the current user in a controller is to type-hint the controller argument with UserInterface (and default it to null if being logged-in is optional):
use Symfony\Component\Security\Core\User\UserInterface\UserInterface; public function indexAction(UserInterface $user = null) { // $user is null when not logged-in or anon. }
This is only recommended for experienced developers who don't extend from the Symfony base controller and don't use the ControllerTrait either. Otherwise, it's recommended to keep using the getUser() shortcut.
Blog post about it
$this->container->get('security.token_storage')->getToken()->getUser();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With