title: Symfony2 - How to use __construct() in a Controller and access Securty.Context?
body: <p>I am having some trouble with Symfony2. Namely in how to use the __construct() function. the Official Documentation is shockingly bad!</p>
  
  <p>I want to be able to use the following:</p>
  
  <pre><code>public function __construct()
  {
      parent::__construct();
      $user = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser();
  }
  </code></pre>
  
  <p>How ever I get the following error:</p>
  
  <blockquote>
    <p>Fatal error: Cannot call constructor in /Sites/src/DEMO/DemoBundle/Controller/Frontend/HomeController.php on line 11</p>
  </blockquote>
  
  <p>Line 11 is "parent::__construct();"</p>
  
  <p>I removed it and got the following, new error</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function get() on a non-object in /Sites/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 242</p>
  </blockquote>
  
  <p>I <em>think</em> I might need to set up the ContainerInterface DIC, but I have no idea how to do this (I tried and failed, miserably)</p>
  
  <p>Any ideas folks?</p>
  
  <p><strong>Update</strong> - Tried changing to extend ContainerAware and got this error:</p>
  
  <blockquote>
    <p>Fatal error: Class DEMO\DemoBundle\Controller\Frontend\HomeController cannot extend from interface Symfony\Component\DependencyInjection\ContainerAwareInterface in /Sites/src/DEMO/DemoBundle/Controller/Frontend/HomeController.php on line 43</p>
  </blockquote>
  
  <p>Using the following code in the controller:</p>
  
  <pre><code>&lt;?php
  
  namespace DEMO\DemoBundle\Controller\Frontend;
  
  use Symfony\Component\DependencyInjection\ContainerAware;
  
  class HomeController extends ContainerAwareInterface
  {
       protected $container;
  
       public function setContainer(ContainerInterface $container = null)
       {
           $this-&gt;container = $container;
       }
  </code></pre>