Zend Framework Custom Helper category related considerations summary _php instance

Source: Internet
Author: User
Tags php programming zend zend framework

This article describes the considerations related to the Zend Framework customization helper class. Share to everyone for your reference, specific 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, suggesting "View_helper" as part of the prefix: "My_view_helper_ Specialpurpose ". (Note the case) you will need to pass the prefix (without underlining) to Addhelperpath () or Sethelperpath ().
The ② class must have a public method with the same name as the helper class. 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, helper classes should not echo or print or have other forms of output. It only needs to return a value on it. The returned data should be escaped.
④ class files should be named the helper method name, for example, in the "Specialpurpose" example, the file should be saved as "specialpurpose.php".

Put the helper files 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.

Paste 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 ';
  }
}

Action using:

<?php echo $this->doctype ()?>
<?php echo $this->img ()?>
<?php echo $this-> TestHelper ()?>

Additional content, add Addhelperpath in Initview, can be changed to adopt load application. INI file configuration item to configure the path. As follows

 class Bootstrap extends Zend_application_bootstrap_bootstrap {protected function _initd
 Octype () {$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, $h
  Elpername);
 }} $viewRenderer = new Zend_controller_action_helper_viewrenderer ();
 Zend_controller_action_helperbroker::addhelper ($viewRenderer);
 $viewRenderer->setview ($view);
 return $view; }
}

[Production]
phpsettings.display_startup_errors = 1
phpsettings.display_errors = 1
includepaths.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 = 1
phpsettings.display_errors = 1
[development:production]
phpsettings.display_startup_errors = 1
phpsettings.display_errors = 1
Resources.frontController.params.displayExceptions = 1

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with your PHP programming.

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.