title: zf2 - injecting dependencies into controller
body: <p>The latest update of zend-mvc has caused a break in compatibility due to phasing out ServiceLocatorAwareInterface. I use the servicelocator within a controller to dynamically load dependencies, eg:</p>
  
  <pre><code>class IndexController extends AbstractActionController
  /**
   *
   * @return \UserManagement\Form\User\Details
   */
  protected function getUserDetailsForm(){
      return $this-&gt;getFormManager()-&gt;get('User\Details');
  }
  
  /**
   *
   * @return FormElementManager
   */
  protected function getFormManager(){
      return $this-&gt;getServiceLocator()-&gt;get('FormElementManager');
  }
  }
  </code></pre>
  
  <p>This is now raising an exception (E_USER_DEPRECEATED) with the following message:</p>
  
  <blockquote>
    <p>You are retrieving the service locator from within the class
    User\Controller\IndexController. Please be aware that
    ServiceLocatorAwareInterface is deprecated and will be removed in
    version 3.0, along with the ServiceLocatorAwareInitializer. You will
    need to update your class to accept all dependencies at creation,
    either via constructor arguments or setters, and use a factory to
    perform the injections.</p>
  </blockquote>
  
  <p>My question is, what is the best way of getting the forms into the controller? My service layer and other controller-specific dependencies are injected into the constructor, but i don't really want to polluate a constructor with all the forms that a controller <em>may</em> need, nor do i want the overhead of creating form objects that will not be used. The forms cannot be created in the controller ie $form = new Form() as they are also created dynamically eg:
  Module.php</p>
  
  <pre><code>public function getFormElementConfig ()
  {
      return array(
              'factories' =&gt; array(
                      'User\Details' =&gt; function($sm){
                          $userMapper = $sm-&gt;getServiceLocator()-&gt;get('Model\User\Mapper');
                          $form = new \User\Form\Details($userMapper);
                          return $form;               
                      }
                  )
              );
  }
  </code></pre>