views:

237

answers:

3

Ive got a view helper in library/my/view/helper/gravatar and so in any view I can call $this->gravatar($email).

But how can I access this function in the models (or controllers)?

Sorry if its already been asked but Im new and the documentation is bloody awful in parts.

Thanks everyone

+4  A: 

In your controller, you can access ViewHelpers through

$this->view->gravatar($email)

Your model should not call methods from the View, as it would tie the model to the presentation layer. The View may know about the model, but the model should not know about the View.

For Gravatars, there is also a Service and View Helper in the making:

Gordon
So where should this gravatar function go? It needs to be available in parts of my modular website
bluedaniel
@bluedaniel can't tell without knowing your app. If you are certain you need this in the model, e.g. as part of your domain logic, either make Gravatar a Service in your model or add getGravatar() to the class that calls it.
Gordon
its in too many models to be copy/pasted into each class, but where would it go to be a sitewide service?
bluedaniel
@bluedaniel A Service could be a class in the model, (like names for instance GravatarService) with all the methods and logic you think it should have. The Service class could be injected into consuming models to use it or accessed by a ViewHelper. Like I said, hard to say without knowing your app or what you envision GravatarService to do.
Gordon
how could it be injected? Ive just tried an include in the model file but it didnt work. what does it matter what the gravatar function is?
bluedaniel
Your ViewHelpers can access Models and ask them to do all sorts of preliminary operations. Just like your controllers often do. However, the Models should not be view aware as it lessens their flexibility. e.g. What happens if you need to also format your gravatar for braille monitors, if you've put the view formating within the model you can't do that properly. The model should only serve the data necessary for a Braille ViewHelper to generate its output.
mike
ah ok, I might be putting a little too much view into models then. So what about adding html to loops and such, or if statments. The view shouldnt contain too much logic no?
bluedaniel
That's the point of ViewHelpers. Allow you to put all the *presentational* logic you want in methods and simply call them in your views. So, you can put code to generate view items such as menus, tables, lists, etc. in helpers. Helpers can in turn call models for data if they need.
mike
@bluedaniel I think you should pose another question for these kind of things. The comments are no good place to discuss this. And the original question is rather sufficiently answered, I'd say.
Gordon
Your right, in both your points! Thanks for your help, wish stack would automatically turn into a forum at this point!
bluedaniel
A: 

In controller:

$this->view->gravatar();

In model (Gordon is right that you should not do it in general):

Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer')->getView()->gravatar()

or simply share Zend_View instance via Zend_Registry. In case you don't have View instance you can directly instantiate it like $g = new View_Helper_Gravatar(). To load it you can use Zend_Loader_PluginLoader.

Vladimir
A: 

A better way to be sure the "thing" from the view is actually a view helper is to use the method getHelper("helperName");.

  $this->view->getHelper('gravatar');
SandRock