Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Current Symfony versions (Symfony 4, Symfony >=3.2)</strong></p> <p><a href="https://symfony.com/blog/new-in-symfony-3-2-user-value-resolver-for-controllers" rel="noreferrer">Since <strong>Symfony >=3.2</strong></a> you can simply expect a <code>UserInterface</code> implementation to be injected to your controller action directly. You can then call <code>getId()</code> to retrieve user's identifier: </p> <pre class="lang-php prettyprint-override"><code>class DefaultController extends Controller { // when the user is mandatory (e.g. behind a firewall) public function fooAction(UserInterface $user) { $userId = $user-&gt;getId(); } // when the user is optional (e.g. can be anonymous) public function barAction(UserInterface $user = null) { $userId = null !== $user ? $user-&gt;getId() : null; } } </code></pre> <p>You can still use the security token storage as in all Symfony versions since 2.6. For example, in your controller:</p> <pre><code>$user = $this-&gt;get('security.token_storage')-&gt;getToken()-&gt;getUser(); </code></pre> <p>Note that <em>the <code>Controller::getUser()</code> shortcut mentioned in the next part of this answer is deprecated in Symfony 3 and was removed in Symfony 4.0</em></p> <p><strong>Legacy Symfony versions</strong></p> <p>The easiest way to access the user used to be to extend the base controller, and use the shortcut <code>getUser()</code> method (removed in Symfony 4):</p> <pre><code>$user = $this-&gt;getUser(); </code></pre> <p>Since <strong>Symfony 2.6</strong> you can retrieve a user from the security token storage:</p> <pre><code>$user = $this-&gt;get('security.token_storage')-&gt;getToken()-&gt;getUser(); </code></pre> <p><strong>Before Symfony 2.6</strong>, the token was accessible from the security context service instead:</p> <pre><code>$user = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser(); </code></pre> <p>Note that <em>the security context service is deprecated in Symfony 2 and was removed in Symfony 3.0.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload