@@ -1881,29 +1881,63 @@ Fetching the User Object
1881
1881
------------------------
1882
1882
1883
1883
After 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 >`:
1886
1886
1887
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1887
+ .. configuration-block ::
1888
1888
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
1892
1898
{
1893
1899
// usually you'll want to make sure the user is authenticated first,
1894
1900
// 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
+ }
1896
1911
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;
1901
1917
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
+ }
1905
1935
}
1906
- }
1936
+
1937
+ .. note ::
1938
+
1939
+ The ``#[CurrentUser] `` attribute can only be used in controller arguments to
1940
+ retrieve the authenticated user.
1907
1941
1908
1942
Fetching the User from a Service
1909
1943
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments