Zend: How to use a custom function from a view helper in the controller?

℡╲_俬逩灬. 提交于 2019-12-07 05:28:23

问题


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


回答1:


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:

  • Zend_Service_Gravatar proposal
  • Zend_Service_Gravatar sourecode (in incubator)
  • Zend_ViewHelper_Gravatar proposal



回答2:


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');



回答3:


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.



来源:https://stackoverflow.com/questions/2353034/zend-how-to-use-a-custom-function-from-a-view-helper-in-the-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!