title: How to log a user out properly in the controller?
body: <p>I have a problem to log a user out in the controller properly. I use Symfony 2.4
  I did this</p>
  
  <pre><code> $this-&gt;get('security.context')-&gt;setToken(NULL);
   $this-&gt;get('request')-&gt;getSession()-&gt;invalidate();
  </code></pre>
  
  <p>and it does not work at all. This code redirected me to the homepage but I am still the logged in user. I tried 5 times and it worked only once. Once I was redirected to login page then to homepage. I did not find in the symfony documentation anything.</p>
  
  <p>I tried this to - no success</p>
  
  <pre><code> $this-&gt;get('security.context')-&gt;setToken(NULL);
      $this-&gt;get('request')-&gt;getSession()-&gt;invalidate();
  
      $response = new Response();
      $response-&gt;headers-&gt;clearCookie('REMEMBERME');
  
      return $this-&gt;render(
                  '...index.html.twig',
                  array(
                     ...
                  ), $response);
  </code></pre>
  
  <p>I am still the logged in user.
  So, I only want to log the user out without redirection. 
   Why cannot symfony do such a basic thing.</p>
  
  <p><strong>EDIT:</strong> <a href="https://stackoverflow.com/a/22223566/2241432">https://stackoverflow.com/a/22223566/2241432</a>
  My problem is: I must stay at the same controller. I must not redirect to /logout route, I must not return the empty Response.  I have many registrations in the app, so the yml configuration cannot solve my problem. For each registration I need different solution. I only want to destroy user and show the proper twig (never homepage).
  I need this solution</p>
  
  <pre><code>   $this-&gt;get('security.context')-&gt;setToken(NULL);
      $this-&gt;get('request')-&gt;getSession()-&gt;invalidate();
  
      $response = new Response();
      $response-&gt;headers-&gt;clearCookie('REMEMBERME');
  
      return $this-&gt;render(
                  '...index.html.twig',
                  array(
                     ...
                  ), $response);
  </code></pre>
  
  <p>Can someone explain why this does not work? How to log the user out and show proper layout in different controllers?</p>