Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
295 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...