如何将自定义视图助手添加到Zend Framework 2 [英] How to add custom view helpers to Zend Framework 2

查看:81
本文介绍了如何将自定义视图助手添加到Zend Framework 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过问题,在那里我得到了很好的答案. 但是,那是针对beta4的,不再有效.

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

那么我在哪里以及如何在ZF2中添加自己的视图助手?

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

推荐答案

您应将它们添加到view_helpers下的module.config.php中,如下所示:

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',
    ),  
),

在这里,我展示了创建助手的两种方法.如果只需要实例化它们,只需将其名称(包括名称空间)添加为invokables.如果您需要对它们或ServiceManager进行处理,请通过factories关键字创建它们.

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.

这篇关于如何将自定义视图助手添加到Zend Framework 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆