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