title: Symfony2 : extending FrameworkBundle Controller
body: <p>I have a question about Symfony2 Controller extending. For the moment, I was always extending the FrameworkBundle one for every controller in my app. But I'm getting sick of always retrieving the user by making a</p>
  
  <pre><code>$this-&gt;get('security.context')-&gt;getToken()-&gt;getUser()
  </code></pre>
  
  <p>or a</p>
  
  <pre><code>$this-&gt;getDoctrine()-&gt;getEntityManager()
  </code></pre>
  
  <p>each time I need the user or the entity manager (I need them a lot). I wanted to be able to retrieve them just by doing $this->em and $this->user.
  So I decided to create a bundle named MyApp/RootBundle, which contains a new controller extending the FrameworkBundle one. This controller would be extended by every other controller in the app. Here is the code :</p>
  
  <pre><code>&lt;?php
  
  namespace MyApp\RootBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\DependencyInjection\ContainerInterface;
  
  class RootController extends Controller
  {
      protected $em;
  
      protected $user;
  
      public function setContainer(ContainerInterface $container = null)
      {
          parent::setContainer($container);
  
          $this-&gt;onContainerSet();
      }
  
      public function onContainerSet()
      {
          $this-&gt;em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $this-&gt;user = $this-&gt;get('security.context')-&gt;getToken()-&gt;getUser();
      }
  }
  </code></pre>
  
  <p>I couldn't load the $this->em and $this->user in a __construct() function since the container is not loaded at the construction time.</p>
  
  <p>So my questions are :</p>
  
  <ul>
  <li>Is that a good idea or should I continue to make the verbose calls ?</li>
  <li>Would it be better to just create functions getEm() and getUser() that would do the same job ?</li>
  </ul>
  
  <p>Thanks !</p>