How to add custom view helpers to Zend Framework 2(如何向 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?
推荐答案
你应该像这样将它们添加到你的 module.config.php 下的 view_helpers 下:
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 ModuleNameHelperMessageShower();
// do stuff with $sm or the $helper
return $helper;
},
),
'invokables' => array(
'selectmenu' => 'ModuleNameHelperSelectMenu',
'prettyurl' => 'ModuleNameHelperPrettyUrl',
),
),
这里我展示了两种创建助手的方法.如果它们只需要实例化,只需将它们的名称(包括命名空间)添加为 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 添加自定义视图助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何向 Zend Framework 2 添加自定义视图助手
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
