@@ -997,28 +997,21 @@ shown above.
997997-----------------------------
998998
999999After 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::
10041002
10051003 public function indexAction()
10061004 {
10071005 $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
10081006
10091007 $user = $this->getUser();
1010- // or you can also type-hint a method argument with UserInterface: e.g. "UserInterface $user"
10111008 }
10121009
10131010.. tip ::
10141011
10151012 The user will be an object and the class of that object will depend on
10161013 your :ref: `user provider <security-user-providers >`.
10171014
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-
10221015Now you can call whatever methods are on *your * User object. For example,
10231016if your User object has a ``getFirstName() `` method, you could use that::
10241017
@@ -1039,14 +1032,7 @@ It's important to check if the user is authenticated first. If they're not,
10391032``$user `` will either be ``null `` or the string ``anon. ``. Wait, what? Yes,
10401033this is a quirk. If you're not logged in, the user is technically the string
10411034``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.
10501036
10511037The point is this: always check to see if the user is logged in before using
10521038the User object, and use the ``isGranted() `` method (or
@@ -1062,6 +1048,25 @@ the User object, and use the ``isGranted()`` method (or
10621048
10631049 }
10641050
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+
10651070Retrieving the User in a Template
10661071~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10671072
0 commit comments