Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The idea of a service manager is you have the ability to replace the form at runtime. It is all about decoupling and reusability.</p> <p>Let's say you have a module <code>Foo</code> and there is a form <code>Foo\Form\Something</code>. Then you put the <code>Foo</code> module in another application, but you need to switch the form for something else. If you just did <code>$form = new Foo\Form\Something</code> in your controller, you have no simple option to instantiate another form. If you register the form under <code>foo_form_something</code>, the <code>Bar</code> module can then overwrite the service. So <code>foo_form_something</code> does not load <code>Foo\Form\Something</code> but <code>Bar\Form\Something</code>. And there is no change required in the controller.</p> <p>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:</p> <pre><code>namespace MyModule\Controller; class MyController { public function myAction() { $form = $this-&gt;getServiceLocator()-&gt;get('foo_form_something'); } } </code></pre> <p>But this makes testing of your controllers much harder. It is easier to inject the dependency:</p> <pre><code>namespace MyModule\Controller; use Foo\Form\Something as SomethingForm; class MyController { protected $form; public function __construct(SomethingForm $foo) { $this-&gt;form = $foo } public function myAction() { $form = $this-&gt;form; } } </code></pre> <p>And you inject the form in the controller configuration:</p> <pre><code>namespace MyModule; class Module { public function getControllerConfig() { return array( 'factories' =&gt; array( 'MyModule\Controller\MyController' =&gt; function($sm) { $form = $sm-&gt;getServiceLocator()-&gt;get('foo_form_something'); $controller = new MyModule\Controller\MyController($form); return $controller; } ), ); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload