@@ -1881,29 +1881,63 @@ Fetching the User Object
18811881------------------------
18821882
18831883After authentication, the ``User `` object of the current user can be
1884- accessed via the ``getUser() `` shortcut in the
1885- :ref: `base controller <the-base-controller-class-services >`::
1884+ accessed via the :ref: ` #[CurrentUser] < controller-value-resolver-current-user >` attribute or ``getUser() `` shortcut in the
1885+ :ref: `base controller <the-base-controller-class-services >`:
18861886
1887- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1887+ .. configuration-block ::
18881888
1889- class ProfileController extends AbstractController
1890- {
1891- public function index(): Response
1889+ .. code-block :: php-attributes
1890+
1891+ // src/Controller/ProfileController.php
1892+
1893+ use App\Entity\User;
1894+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1895+ use Symfony\Component\Security\Http\Attribute\CurrentUser;
1896+
1897+ class ProfileController extends AbstractController
18921898 {
18931899 // usually you'll want to make sure the user is authenticated first,
18941900 // see "Authorization" below
1895- $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
1901+ #[IsGranted('IS_AUTHENTICATED_FULLY')]
1902+ public function index(
1903+ // returns your User object, or null if the user is not authenticated
1904+ #[CurrentUser] ?User $user
1905+ ): Response {
1906+ // Call whatever methods you've added to your User class
1907+ // For example, if you added a getFirstName() method, you can use that.
1908+ return new Response('Well hi there '.$user->getFirstName());
1909+ }
1910+ }
18961911
1897- // returns your User object, or null if the user is not authenticated
1898- // use inline documentation to tell your editor your exact User class
1899- /** @var \App\Entity\User $user */
1900- $user = $this->getUser();
1912+ .. code-block :: php
1913+
1914+ // src/Controller/ProfileController.php
1915+
1916+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
19011917
1902- // Call whatever methods you've added to your User class
1903- // For example, if you added a getFirstName() method, you can use that.
1904- return new Response('Well hi there '.$user->getFirstName());
1918+ class ProfileController extends AbstractController
1919+ {
1920+ public function index(): Response
1921+ {
1922+ // usually you'll want to make sure the user is authenticated first,
1923+ // see "Authorization" below
1924+ $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
1925+
1926+ // returns your User object, or null if the user is not authenticated
1927+ // use inline documentation to tell your editor your exact User class
1928+ /** @var \App\Entity\User $user */
1929+ $user = $this->getUser();
1930+
1931+ // Call whatever methods you've added to your User class
1932+ // For example, if you added a getFirstName() method, you can use that.
1933+ return new Response('Well hi there '.$user->getFirstName());
1934+ }
19051935 }
1906- }
1936+
1937+ .. note ::
1938+
1939+ The ``#[CurrentUser] `` attribute can only be used in controller arguments to
1940+ retrieve the authenticated user.
19071941
19081942Fetching the User from a Service
19091943~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments