問題描述
我之前問過這個問題,我在那里得到了很好的答案.但是,這是針對 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 添加自定義視圖助手的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!