How to add custom view helpers to Zend Framework 2

Answer

add view helpers to 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 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 we need to do stuff with them or the ServiceManager, create them through the factories keyword.

All zend Framework 2 Questions

Ask your interview questions on zend-framework-2

Write Your comment or Questions if you want the answers on zend-framework-2 from zend-framework-2 Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---