How we can add custom view helpers to Zend Framework 2?
To create custom view helpers to Zend framework 2, follow these steps:
- Create a helper under the src directory. In this case, we name it Testhelper.
- Build Testhelper content to:
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';
}
}
- Now register it in Module.php
- Call it in the view
echo $this->test_helper("me","e");
BY Best Interview Question ON 22 Feb 2019