Zend Framework Custom View Helper
3 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(); ?>


















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.