This article describes the considerations for Zend Framework Custom helper classes. Share to everyone for your reference, as follows:
Writing a custom helper class
Writing a custom helper class is easy, as long as you follow these guidelines:
The ① class name must be zend_view_helper_*,* is the name of the Helper. For example, you are writing a class called "Specialpurpose", the class name is at least "specialpurpose", and you should prefix the class name with the suggestion of "View_helper" as part of the prefix: "My_view_helper_ Specialpurpose ". (note capitalization) you will need to pass the prefix (without underlining) to Addhelperpath () or Sethelperpath ().
There must be a public method in the ② class that has the same name as the helper class name. This method will be executed when your template calls "$this->specialpurpose ()". In our "Specialpurpose" example, the corresponding method declaration can be "public function specialpurpose ()".
③ in general, the helper class should not echo or print or have other forms of output. It just needs to return a value. The returned data should be escaped.
The naming of the ④ class file should be the name of the helper method, such as in the "Specialpurpose" example, where the file is saved as "specialpurpose.php".
Put the helper file under your helper path and the Zend_view will automatically load, instantiate, persist, and execute.
Three-point class file name, class name, helper method in class, to maintain a certain degree of consistency.
Post code:
Two helper, see clearly, they are different ah ...
Version ZF 1.10
bootstrap.php
Class Bootstrap extends Zend_application_bootstrap_bootstrap { protected function _initdoctype () { $this Bootstrap (' view '); $view = $this->getresource (' view '); $view->doctype (' xhtml1_strict '); } protected function _initview () { $view = new Zend_view (); $view->setencoding (' UTF-8 '); $view->doctype (' xhtml1_strict '); $view->addhelperpath ('.. /application/views/helpers ', ' my_view_helper '); $viewRenderer = new Zend_controller_action_helper_viewrenderer (); Zend_controller_action_helperbroker::addhelper ($viewRenderer); $viewRenderer->setview ($view); return $view; }}
Application/views/helpers
img.php:
Class Zend_view_helper_img extends zend_view_helper_abstract{public function Img () { return ' this is a IMG "; }}
testhelper.php:
Class My_view_helper_testhelper extends zend_view_helper_abstract{public function TestHelper () { Return "This is a TestHelper"; }}
Use in action:
<?php echo $this->doctype ()? ><?php echo $this->img ()? ><?php echo $this->testhelper ()?>
Additional content, add Addhelperpath in Initview, can be changed to take load application. INI file configuration item to configure the path. As follows
class Bootstrap extends Zend_application_bootstrap_bootstrap {protected function _initdoctype () {$this->bootstrap (' View '), $view = $this->getresource (' view '); $view->doctype (' X Html1_strict '); } protected function _initview () {$view = new Zend_view (); $view->setencoding (' UTF-8 '); $view->doctype (' XHTML 1_strict '); $options = $this->getoptions (); $viewOptions = $options [' Resources '] [' view '] [' helperpath ']; if (Is_array ($viewOptions)) {foreach ($viewOptions as $helperName = = $path) {$view->addhelperpath ($path, $help Ername); }} $viewRenderer = new Zend_controller_action_helper_viewrenderer (); Zend_controller_action_helperbroker::addhelper ($viewRenderer); $viewRenderer->setview ($view); return $view; }}
[production]phpsettings.display_startup_errors = 1phpsettings.display_errors = 1includepaths.library = APPLICATION_ PATH "/.. /library "Bootstrap.path = Application_path"/bootstrap.php "Bootstrap.class =" bootstrap "Appnamespace =" APPLICATION " resources.view[] =resources.view.helperpath.my_view_helper = "... /application/views/helpers "resources.frontController.controllerDirectory = Application_path"/controllers " Resources.frontController.params.displayExceptions = 1[staging:production][testing:production] Phpsettings.display_startup_errors = 1phpsettings.display_errors = 1[development:production]phpsettings.display_ Startup_errors = 1phpsettings.display_errors = 1resources.frontcontroller.params.displayexceptions = 1
More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
I hope this article is helpful to you in PHP programming.