I’ve been working through the ZF2 tutorial and I’m trying to rework the indexAction method in the Controller so I do not need to create a new ViewModel object every time.
I added an onBootstrap method to the Module with the following code:
$di = new \Zend\Di\Di;
$di->instanceManager()->setParameters('Album\ViewModel', array('albums' => $config- >albums));
And I changed the indexAction method to:
return $di->get('Album\ViewModel');
Can anyone tell me where I’m going wrong or what I should be doing instead?
You can just return an array from Actions inside Controllers and ZF2’ll create a ViewModel around it itself. Not quite sure that’s what you’re after, but it might be.