I am developing an application using Symfony2 and doctrine 2. I would like to know how can I get the currently logged in user's Id.
相关问题
- Symfony2 Set Controller in the kernelControllerEve
- Webpack Encore: cannot import local dependencies
- Render custom attribute KNP Menu
- Problems with cap deploy a symfony2 project, can
- Allow CORS on symfony 4
相关文章
- Symfony : Doctrine data fixture : how to handle la
- Symfony is linked to the wrong PHP version
- Symfony2: check whether session exists or not
- Is there a way to modify the entity mapping config
- symfony2 form choice and mongodb
- Get random records with Doctrine
- Symfony 3.1 and OneUpUploaderBundle + Blueimp = Up
- Doctrine not finding data on Google App Engine?
In symfony2, we can get this simpler by this code:
You can get the variable with the code below:
This can get dressed in the method:
And induction $this->getUserId()
Current Symfony versions (Symfony 4, Symfony >=3.2)
Since Symfony >=3.2 you can simply expect a
UserInterface
implementation to be injected to your controller action directly. You can then callgetId()
to retrieve user's identifier:You can still use the security token storage as in all Symfony versions since 2.6. For example, in your controller:
Note that the
Controller::getUser()
shortcut mentioned in the next part of this answer is deprecated in Symfony 3 and was removed in Symfony 4.0Legacy Symfony versions
The easiest way to access the user used to be to extend the base controller, and use the shortcut
getUser()
method (removed in Symfony 4):Since Symfony 2.6 you can retrieve a user from the security token storage:
Before Symfony 2.6, the token was accessible from the security context service instead:
Note that the security context service is deprecated in Symfony 2 and was removed in Symfony 3.0.