Zend Framework 2 custom view helpers - cross modules

*爱你&永不变心* 提交于 2019-12-19 11:22:11

问题


In Zend Framework 2, how can I have one view helper available to multiple modules?

What I want is to have some general functions, like algorithmic functions, that can be reused by multiple modules.

I'm currently on ZF 2 2.0.3

Thanks.


回答1:


Its simple, put it in one of your modules. You can use classes and functions from other modules in ZF2. Because modules in ZF2 aren't much more than just namespaces, with some classes in them. So, if you create a view helper with the name \Module\View\Helper\MyHelper, you can simply use that view helper in another module.




回答2:


In addition to what kokx said, you will also need to add service manager configuration for the view helper manager. This can be done in one of two ways: in your module configuration, or in your Module class.

// module configuration:
return array(
    'view_helpers' => array(
        'invokables' => array(
            'dimmy' => 'DimmyUtil\View\Helper\DimmyUtil',
        ),
    ),
);

or, in a Module class:

namespace DimmyUtil;

class Module
{
     public function getViewHelperConfig()
     {
         return array('factories' => array(
             'dimmy' => function ($helpers) {
                  // do some stuff...
                  return $helper;
             }
         ));
     }
}

Once you do so, the view helper will be available throughout any application composing the module.



来源:https://stackoverflow.com/questions/14201244/zend-framework-2-custom-view-helpers-cross-modules

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