ZendFramework custom Helper class considerations summary, zendhelper. ZendFramework custom Helper class considerations summary, zendhelper this article describes ZendFramework custom Helper class related considerations. This article is for your reference. The following is a summary of precautions for the Zend Framework custom Helper class. zendhelper
This article describes precautions for the Zend Framework custom Helper class. We will share this with you for your reference. The details are as follows:
Compile a custom Helper class
It is easy to compile a custom Helper class. follow the following principles:
① The class name must be Zend_View_Helper _ *, and * is the name of helper. For example, if you are writing a class named "specialPurpose", the class name should at least be "SpecialPurpose", and you should add a prefix to the class name, we recommend that you use "View_Helper" as a prefix: "My_View_Helper_SpecialPurpose ". (Case sensitive) you will need to pass the prefix (excluding the underline) to addHelperPath () or setHelperPath ().
② The class must have a public method. the method name is the same 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 only needs to return values. The returned data should be escaped.
④ The class file name should be the helper method name. for example, in the "specialPurpose" example, the file should be saved as "SpecialPurpose. php ".
Put the helper class file under your helper path, Zend_View will be automatically loaded, instantiated, persistent, and executed.
The three class file names, class names, and the helper method in the class must be consistent to some extent.
Code:
Two helper. they are different .....
Versions 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() ?>
Add addHelperPath to initView. you can change it to load application. Configure the path in the way of configuration items in the INI file. As follows:
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' ); $options = $this->getOptions (); $viewOptions = $options ['resources']['view']['helperPath']; if (is_array ($viewOptions)) { foreach($viewOptions as $helperName =>$path) { $view->addHelperPath ( $path, $helperName ); } } $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