Zend Framework custom Helper class precautions, zendhelper

Source: Internet
Author: User
Tags zend framework

Zend Framework custom Helper class precautions, 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.