@@ -994,28 +994,21 @@ shown above.
994
994
-----------------------------
995
995
996
996
After authentication, the ``User `` object of the current user can be accessed
997
- via the ``security.token_storage `` service. From inside a controller, this will
998
- look like::
999
-
1000
- use Symfony\Component\Security\Core\User\UserInterface;
997
+ via the ``getUser() `` shortcut (which uses the ``security.token_storage ``
998
+ service). From inside a controller, this will look like::
1001
999
1002
1000
public function indexAction()
1003
1001
{
1004
1002
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
1005
1003
1006
1004
$user = $this->getUser();
1007
- // or you can also type-hint a method argument with UserInterface: e.g. "UserInterface $user"
1008
1005
}
1009
1006
1010
1007
.. tip ::
1011
1008
1012
1009
The user will be an object and the class of that object will depend on
1013
1010
your :ref: `user provider <security-user-providers >`.
1014
1011
1015
- .. versionadded :: 3.2
1016
- The ability to get the user by type-hinting an argument with UserInterface
1017
- was introduced in Symfony 3.2.
1018
-
1019
1012
Now you can call whatever methods are on *your * User object. For example,
1020
1013
if your User object has a ``getFirstName() `` method, you could use that::
1021
1014
@@ -1036,14 +1029,7 @@ It's important to check if the user is authenticated first. If they're not,
1036
1029
``$user `` will either be ``null `` or the string ``anon. ``. Wait, what? Yes,
1037
1030
this is a quirk. If you're not logged in, the user is technically the string
1038
1031
``anon. ``, though the ``getUser() `` controller shortcut converts this to
1039
- ``null `` for convenience. When type-hinting the
1040
- :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ UserInterface\\ UserInterface `
1041
- and being logged-in is optional, you can allow a null value for the argument::
1042
-
1043
- public function indexAction(UserInterface $user = null)
1044
- {
1045
- // $user is null when not logged-in or anon.
1046
- }
1032
+ ``null `` for convenience.
1047
1033
1048
1034
The point is this: always check to see if the user is logged in before using
1049
1035
the User object, and use the ``isGranted() `` method (or
@@ -1059,6 +1045,25 @@ the User object, and use the ``isGranted()`` method (or
1059
1045
1060
1046
}
1061
1047
1048
+ .. note ::
1049
+
1050
+ An alternative way to get the current user in a controller is to type-hint
1051
+ the controller argument with
1052
+ :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ UserInterface\\ UserInterface `
1053
+ (and default it to ``null `` if being logged-in is optional)::
1054
+
1055
+ use Symfony\Component\Security\Core\User\UserInterface\UserInterface;
1056
+
1057
+ public function indexAction(UserInterface $user = null)
1058
+ {
1059
+ // $user is null when not logged-in or anon.
1060
+ }
1061
+
1062
+ This is only recommended for experienced developers who don't extend from the
1063
+ :ref: `Symfony base controller <the-base-controller-class-services >` and
1064
+ don't use the :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ ControllerTrait `
1065
+ either. Otherwise, it's recommended to keep using the ``getUser() `` shortcut.
1066
+
1062
1067
Retrieving the User in a Template
1063
1068
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1064
1069
0 commit comments