Zend Framework Custom Helper Class Considerations Summary, zendhelper_php Tutorial

Source: Internet
Author: User
Tags zend framework

Zend Framework Custom Helper Class considerations Summary, Zendhelper


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 (' xhtml1_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.

Articles you may be interested in:

    • Zend Framework Tutorial Bootstrap Class Usage Overview
    • Zend Framework Tutorial Resource (resources) usage examples
    • Zend Framework Tutorial application and bootstrap usage
    • Zend Framework Tutorial configuration file Application.ini parsing
    • Zend Framework Tutorial Loader and Pluginloader usage
    • Zend Framework Tutorial Autoloading Usage
    • Examples of resource autoloading usages of the Zend framework tutorial
    • Controller usage Analysis of MVC Framework for Zend Framework Tutorial
    • Zend Framework Tutorial's routing function zend_controller_router detailed
    • Zend Framework Tutorial Zend_controller_plugin plugin Usage
    • Package Zend_controller_response Example of response object for Zend Framework tutorial
    • Zend Framework Tutorial Action base class Zend_controller_action detailed
    • Zend Framework Tutorial Front Controller Zend_controller_front Usage
    • Zend Framework Tutorial Application usage examples

http://www.bkjia.com/PHPjc/1110090.html www.bkjia.com true http://www.bkjia.com/PHPjc/1110090.html techarticle Zend Framework Custom helper Classes Related considerations Summary, Zendhelper This article describes the Zend Framework custom helper class considerations. Share to everyone for your reference, as follows ...

  • 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.