Zend Framework Custom View Helper
9 Comments 14th SEP 2009 | Posted by Vedran Subotic in Zend

Creating custom helper in new Zend Framework edition 1.9 in few easy steps.
Let’s assume that we need to fetch some data from database
but we do not need them in all view files.
We need to insert this line in application/config.ini to register path to helpers:
application/configs/application.ini
resources.view.helperPath = APPLICATION_PATH “/views/helpers”
First create model which will give you an array,
in my example it’s list of states, which you will call in you helper file:
application/models/DbTable/States.php
class Model_DbTable_States extends Zend_Db_Table_Abstract
{
protected $_name = 'state_list';
public function getStates()
{
$row = $this->fetchAll();
if (!$row) {
throw new Exception("No States");
}
$row = $row->toArray();
return $row;
}
}
Class needs to begegins with “Zend_View_Helper_”
That is why it is “view helper” and it cannot be called in controller or model files,
if you do that you will get error that “method deoesn’t exists”.
application/view/helpers/State.php
class Zend_View_Helper_State
{
protected $_list;
public function state()
{
$state = new Model_DbTable_States();
return $this->_list->getStates();
}
}
After that and make a list of states in:
application/layouts/sidebar.phtml
<ul> < ?php foreach ($this->state() as $item):?> <li>< ?php echo $item['state']?></li> < ?php endforeach;?> </ul>
and call it in main layout file:
application/layouts/layout.phtml
< ?php
echo $this->partial('sidebar.phtml');
?>
In this case you will get content from your helper file in all views which will render main layout file from application/layouts/layout.phtml.
However, if you wish to call your helper in particular view all you need to do is to call the helper in view file:
< ?php $this->state(); ?>
To post code in comments, place your code inside [code] and [/code] tags.


















January 4th, 2010 at 18:05
great great tip, unfortunately I’ve get here after I’ve spent few hours on searching how this should be done.
January 23rd, 2010 at 0:40
I dont think that is a valid MVC aproach to fetch model in the view helper. This is the task of application logic – controllers. After you can wrap data using view helpers.
February 14th, 2010 at 20:11
Well yes and no IMHO.
The controller’s job is to service the request.
When that logic has nothing to do with some other part of a page then there is no need to complicate your controllers doing stuff that maybe belongs more to the view or layout, than the request itself.
The controller needs to control what should be under its control – the request.
But the view should only be accessing data for read purposes in that case, not for writing, and there is one danger – a proxy object would make it safer to do this kind of thing so the view is not given full update access to the model.
Current thinking seems to be that one way to organise this kind of view+model oriented logic is to have a service layer or service class as a kind of middleware I guess, that can dispense aspects of the model to views (or helpers), and also helps to abstract the ORM stuff.
But its not in the ZF yet of course.
So having said that, I would agree that when there is data that is directly involved with the request per se, then first there should be a conversation between the controller and the model and the view should wait patiently until that is done.
June 26th, 2010 at 21:06
Congrats! You were the only one on the whole internet who could explain this in plain english. Come on all you ZF people out there and follow this example
July 30th, 2010 at 1:57
Please Please take the Windows OS double quotes out of your post! People – like me – will spend hours trying to wonder what is wrong, then give up and never use your solution.
July 30th, 2010 at 8:00
@Jake Noble
Form post text or from code sample?
I just don’t see the issue here,
part you need is code sample which work on all platforms.
July 30th, 2010 at 11:52
@vedran This line which is not marked as code in your example
resources.view.helperPath = APPLICATION_PATH “/views/helpers”This line is code even if it’s not in code tags, it’s useful. I found this page after Googling for an effective way of adding view helper directories to an app.
Food that isn’t on a plate is still food?
July 30th, 2010 at 12:07
@Jake Noble
yea, I’m also mad at “Windows OS double quotes” when I used to copy-paste other’s people free code samples..
But this is something that WordPress does and I’m not supposed to take care of “Windows OS double quotes”
July 30th, 2010 at 13:45
Ah, WordPress. The savior and bane of so many.
Anyway, once I removed the double quotes it was what I needed