I would like to know the differences b开发者_如何学运维etween two helpers Zend_Controller_Action_Helper_Abstract
and Zend_View_Helper_Abstract
in Zend framework which I did not understand.
Like their names suggest:
An ActionHelper is for reusable functionality needed in the actions of your Controllers, while a ViewHelper is for reusable functionality in your View Templates.
From http://zendframework.com/manual/en/zend.controller.actionhelpers.html
Action Helpers allow developers to inject runtime and/or on-demand functionality into any Action Controllers that extend Zend_Controller_Action. Action Helpers aim to minimize the necessity to extend the abstract Action Controller in order to inject common Action Controller functionality.
From http://zendframework.com/manual/en/zend.view.helpers.html
In your view scripts, often it is necessary to perform certain complex functions over and over: e.g., formatting a date, generating form elements, or displaying action links. You can use helper classes to perform these behaviors for you.
The main idea is to encapsulate often used functionality into objects, instead of recreating that functionality over again or using inheritance for it (which would bloat the base class and violate the Single Responsibility Principle). ZF distinguishes between ViewHelpers and ActionHelpers because they have different responsibility. ActionHelpers are used in helping handling a request, while ViewHelpers are used in helping rendering the model.
Simple rule of thumb if your view helper needs to know about your model (using Zend_Db_Table) then you are doing it wrong instead use action helper . Action helper benefits you have access to hooks such as preDispatch and postDispatch.
View helper benefits you can use it inside action controller also e.g echo $this->view->myHelper();
And I have see this used very off-ten.
精彩评论