i want to create a View Helper named
Testhelper which the responsibility of this helper is to state that the string is found or not. First of all, we create a Helper under src directory :
And the content of Testhelper is :
namespace Test\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Testhelper extends AbstractHelper
{
public function __invoke($str, $find)
{
if (! is_string($str)){
return 'must be string';
}
if (strpos($str, $find) === false){
return 'not found';
}
return 'found';
}
}
The next step is register that in Module.php
namespace Test;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface,
Zend\ModuleManager\Feature\ConfigProviderInterface,
Zend\ModuleManager\Feature\ViewHelperProviderInterface;
class Module implements
AutoloaderProviderInterface,
ConfigProviderInterface,
ViewHelperProviderInterface
{
public function getAutoloaderConfig(){/*common code*/}
public function getConfig(){ /*common code */ }
public function getViewHelperConfig()
{
return array(
'factories' => array(
'test_helper' => function($sm) {
$helper = new View\Helper\Testhelper ;
return $helper;
}
)
);
}
}
OR, you can SIMPLIFY that by configuring it as an invokable in module.config.php as @weierophinney ( project lead for Zend Framework ) suggestion –thanks a lot for his suggestion– like this :
return array(
//.........
'view_helpers' => array(
'invokables'=> array(
'test_helper' => 'Test\View\Helper\Testhelper'
)
),
//.........
);
Last step, call it in the view :
echo $this->test_helper("me","e");