Question

i am just getting my hands on zend framework 2. I have created a user form and to use that form in my controller i can either include the from class in my controller and create and object using new or i can get access to form using service manager config to access the form.

i am finding reasons for using service manager here rather than directly instantiating the object. any ideas??

Was it helpful?

Solution

The idea of a service manager is you have the ability to replace the form at runtime. It is all about decoupling and reusability.

Let's say you have a module Foo and there is a form Foo\Form\Something. Then you put the Foo module in another application, but you need to switch the form for something else. If you just did $form = new Foo\Form\Something in your controller, you have no simple option to instantiate another form. If you register the form under foo_form_something, the Bar module can then overwrite the service. So foo_form_something does not load Foo\Form\Something but Bar\Form\Something. And there is no change required in the controller.

There is another related coding style, where you inject the form in your controller instead of pulling it via the service manager. You perhaps use this:

namespace MyModule\Controller;

class MyController
{
  public function myAction()
  {
    $form = $this->getServiceLocator()->get('foo_form_something');
  }
}

But this makes testing of your controllers much harder. It is easier to inject the dependency:

namespace MyModule\Controller;

use Foo\Form\Something as SomethingForm;

class MyController
{
  protected $form;

  public function __construct(SomethingForm $foo)
  {
    $this->form = $foo
  }

  public function myAction()
  {
    $form = $this->form;
  }
}

And you inject the form in the controller configuration:

namespace MyModule;

class Module
{
  public function getControllerConfig()
  {
    return array(
      'factories' => array(
        'MyModule\Controller\MyController' => function($sm) {
          $form       = $sm->getServiceLocator()->get('foo_form_something');
          $controller = new MyModule\Controller\MyController($form);

          return $controller;
        }
      ),
    );
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top