I successfully added a custom view helper with the following configuration:
The view helper has a custom variable named
As I mentioned this setup works fine as long as I have only a single element using the view helper.
As soon as I add more than one form element the
setFoo method gets only called once and the
foo variable remains set throughout the following elements.
I read about Shared Services in ZF2 - is this such a case? How can I prevent this behaviour?
Introduction to the Zend Framework 2 ServiceManager
By default, the ServiceManager assumes all services are shared, but you may specify a boolean false value here to indicate a new instance should be returned.
Best How To :
You've basically answered your own question here. Services are shared by default, so the initial instance of your view helper is reused for subsequent calls unless you configure it otherwise. To do this, add the
shared parameter to your view helper config (untested):
public function getViewHelperConfig()
'invokables' => array(
'formelement' => 'Application\Form\View\Helper\FormElement',
'formautocompletehidden' => 'Application\Form\View\Helper\FormAutocompleteHidden'
'shared' => array(
'formelement' => false,
'formautocompletehidden' => false
Edit: as mentioned in the comments, this doesn't currently work for view helpers due to a bug in ZF2.