How to add custom view helpers to Zend Framework 2

让人想犯罪 __ 提交于 2019-11-26 21:03:29

问题


I have earlier asked this question, and I got good answers there. However, that was for beta4, and no longer works.

So where and how do I add my own view helpers to ZF2?


回答1:


You should add them to your module.config.php under view_helpers like this:

'view_manager' => array(
    'template_path_stack' => array(
        'ModuleName' => __DIR__ . '/../view',
    ),
),

'view_helpers' => array(
    'factories' => array(
        'showmessages' => function($sm) {
            $helper = new ModuleName\Helper\MessageShower();
            // do stuff with $sm or the $helper
            return $helper;           
        },
    ),
    'invokables' => array(
        'selectmenu' => 'ModuleName\Helper\SelectMenu',   
        'prettyurl'  => 'ModuleName\Helper\PrettyUrl',
    ),  
),

Here I show two ways of creating the helpers. If all they need to do is to be instantiated, just add their name (including namespace) as invokables. If you need to do stuff with them or the ServiceManager, create them through the factories keyword.




回答2:


The beta5 had a BC regarding the servicemanager. This applies for the view helper manager as well. Have a look here - there's even an example for view helpers a bit down too.



来源:https://stackoverflow.com/questions/11392624/how-to-add-custom-view-helpers-to-zend-framework-2

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