ZendFramework 2 - Create a custom view helper
For many situations, we need some helper functions to be used in view.
i.e. When you want to debug, probably you will write
./module/Application/view/application/index/foo.phtml
1 | <div class="whatever"> |
Using <pre>
to wrap it will be more readable, unfortunately you don’t want to every time write such a long line. Now, create a helper to be able to use in all views.
Create a new file ./module/Application/src/Application/View/Helper/God.php (Let’s call it God
as this helper can do anything that you want provided you defined it as a function inside)
1 |
|
Now you have to edit your ./module/Application/Module.php in order for it to load to view
1 |
|
godHelper
is an alias to be used in your view.
Now you’re done, you can use in any view now. i.e.
./module/Application/view/application/index/foo.phtml
1 | <div class="whatever"> |
You can now add as many functions as you want to the God
class.
Reference: How to write custom view helper in Zend Framework 2 ?