As you can see by its name this is a class that you can call or access it in your view file.
So when you need a class in your view file like a function to calculate total or anything else.
You need to create “view helper” it’s difference with "action helper" because "Action helper" is intended to just call from action controller.
Step 1:
-----------------------------------------------------------------------
1. create a new directory and new file.
-----------------------------------------------------------------------
your_module_name/src/your_module_name/view/Helper/Foohelper.php
e.g Suppose you want to create a helper for User Module, file path should be like this
module/User/src/User/view/Helper/Foohelper.php
Step 2:
-----------------------------------------------------------------------
Add new file in it :
-----------------------------------------------------------------------
Step 3:
<?php
namespace User\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Foohelper extends AbstractHelper
{
public function __invoke()
{
return $this;
}
public function fooMethod( $test ){
echo $test;
}
public function barMethod(){
echo 'hai';
}
}
-----------------------------------------------------------------------
Edit your Module.php Of User Module
-----------------------------------------------------------------------
public function getViewHelperConfig()
{
return array(
'factories' => array(
'foo_helper' => function($sm) {
$helper = new View\Helper\Foohelper ;
return $helper;
}
)
);
}
Step 4:
-----------------------------------------------------------------------
How to use it in your view file :
-----------------------------------------------------------------------
<?php echo $this->foo_helper()->fooMethod('hello'); ?>
No comments:
Post a Comment