title: Zend Framework and controller injection to solve tight coupling
body: <p>Similar to ASP.NET MVC, I would like to achieve loose coupling on the controller and their dependencies.</p>
  
  <p>I would like to achieve the following:</p>
  
  <pre><code>class TimesheetController extends AbstractActionController
  {    
      private $userRepository;
  
      function __Construct()
      {
          $sm = $this-&gt;getServiceLocator();
          $this-&gt;userRepository = $sm-&gt;get('Application\Model\Concrete\TGUserRepository');
      }
  
      // ... becomes
  
      function __Construct( IUserRepository $userRepo ) // the controller is injected with a bound repository so I do not have to implicitly specify it in the construct
      {
          $this-&gt;userRepository = $userRepo;
      }
  
      // And this is my action
  
      public function ListAction()
      {
          $users = $this-&gt;userRepository-&gt;Users();
  
          $view = new ViewModel( array( "Users" =&gt; $users ) );
  
              $secondarySidebarView = new ViewModel();
              $secondarySidebarView-&gt;setTemplate('something/poo');
  
          $view-&gt;addChild($secondarySidebarView, 'something');
          return $view;
      }
  
      // .... and more
  }
  </code></pre>
  
  <p>How do I go about doing this?</p>
  
  <p>I would imagine there would be a binding function that says when something needs IUserRepository, go make a TGUserRepository</p>
  
  <pre><code>Bind("IUserRepository", "TGUserRepository");
  </code></pre>
  
  <p>I have seen Laravel does it exactly how I want:</p>
  
  <p><a href="http://laravel.com/docs/ioc" rel="nofollow">http://laravel.com/docs/ioc</a></p>