multiple instances of view helper plugin?

时光怂恿深爱的人放手 提交于 2019-12-22 11:46:51

问题


How do I instantiate multiple instances of a view helper plugin in Zend 2?

I want to return a new instance every time I call $this->pluginName(); from the view.

How do I return a new instance of the view plugin?


回答1:


Add the service name to the getViewHelperConfig() shared configuration key in Module.php and set this value to false

Module.php

function getViewHelperConfig()
{
  return array(
    'shared' => array(
      'MyViewHelper' => false,
    ),
    'factories' => array(
      'MyViewHelper' => 'App\View\Helper\MyViewHelperFactory',
    )
  );
}

By adding 'MyViewHelper' => false, the service manager (or View Helper plugin manager) will create a new instance of that service each time it is used.

The documentation states

shared An array of service name/boolean pairs, indicating whether or not a service should be shared. 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.



来源:https://stackoverflow.com/questions/21272055/multiple-instances-of-view-helper-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!