@@ -1881,29 +1881,65 @@ 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+         namespace App\Controller; 
1893+ 
1894+         use App\Entity\User; 
1895+         use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 
1896+         use Symfony\Component\Security\Http\Attribute\CurrentUser; 
1897+ 
1898+         class ProfileController extends AbstractController 
18921899        { 
18931900            // usually you'll want to make sure the user is authenticated first, 
18941901            // see "Authorization" below 
1895-             $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); 
1902+             #[IsGranted('IS_AUTHENTICATED_FULLY')] 
1903+             public function index( 
1904+                 // returns your User object, or null if the user is not authenticated 
1905+                 #[CurrentUser] ?User $user 
1906+             ): Response { 
1907+                 // Call whatever methods you've added to your User class 
1908+                 // For example, if you added a getFirstName() method, you can use that. 
1909+                 return new Response('Well hi there '.$user->getFirstName()); 
1910+             } 
1911+         } 
18961912
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() ; 
1913+ ..  code-block ::  php 
1914+ 
1915+         // src/Controller/ProfileController.php  
1916+         namespace App\Controller ; 
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+         use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 
1919+ 
1920+         class ProfileController extends AbstractController 
1921+         { 
1922+             public function index(): Response 
1923+             { 
1924+                 // usually you'll want to make sure the user is authenticated first, 
1925+                 // see "Authorization" below 
1926+                 $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); 
1927+ 
1928+                 // returns your User object, or null if the user is not authenticated 
1929+                 // use inline documentation to tell your editor your exact User class 
1930+                 /** @var \App\Entity\User $user */ 
1931+                 $user = $this->getUser(); 
1932+ 
1933+                 // Call whatever methods you've added to your User class 
1934+                 // For example, if you added a getFirstName() method, you can use that. 
1935+                 return new Response('Well hi there '.$user->getFirstName()); 
1936+             } 
19051937        } 
1906-     } 
1938+ 
1939+ note ::
1940+ 
1941+     The ``#[CurrentUser] `` attribute can only be used in controller arguments to
1942+     retrieve the authenticated user.
19071943
19081944Fetching the User from a Service
19091945~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
0 commit comments