>  기사  >  php教程  >  Zend Framework Custom Helper 클래스 관련 주의사항 요약

Zend Framework Custom Helper 클래스 관련 주의사항 요약

高洛峰
高洛峰원래의
2016-12-27 14:23:581447검색

본 글에서는 Zend Framework Custom Helper 클래스 관련 주의사항을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

사용자 정의 Helper 클래스 작성

다음 원칙을 따르는 한 사용자 정의 Helper 클래스를 작성하는 것은 쉽습니다.

① 클래스 이름은 Zend_View_Helper_*이어야 하며, *는 도우미 이름입니다. 예를 들어, "specialPurpose"라는 클래스를 작성하는 경우 클래스 이름은 최소한 "SpecialPurpose"이어야 합니다. 또한 접두사 부분으로 "View_Helper"를 사용하는 것이 좋습니다. "My_View_Helper_SpecialPurpose". (참고 사례) addHelperPath() 또는 setHelperPath()에 접두어(밑줄 없이)를 전달해야 합니다.
② 클래스에는 퍼블릭 메소드가 있어야 하며, 메소드 이름은 헬퍼 클래스 이름과 동일합니다. 이 메서드는 템플릿이 "$this->specialPurpose()"를 호출할 때 실행됩니다. "specialPurpose" 예제에서 해당 메서드 선언은 "public function SpecialPurpose()"일 수 있습니다.
3 일반적으로 Helper 클래스는 에코나 인쇄 등 다른 형식의 출력을 가져서는 안 됩니다. 값을 반환하기만 하면 됩니다. 반환된 데이터는 이스케이프되어야 합니다.
IV 클래스 파일 이름은 헬퍼 메소드 이름이어야 합니다. 예를 들어 "specialPurpose" 예제에서는 파일이 "SpecialPurpose.php"로 저장되어야 합니다.

헬퍼 클래스 파일을 헬퍼 경로에 넣으면 Zend_View가 자동으로 로드, 인스턴스화, 지속 및 실행됩니다.

3점으로 된 클래스 파일명, 클래스명, 클래스 내 헬퍼 메소드는 어느 정도 일관되어야 합니다.

코드 게시:

두 명의 도우미, 차이점을 명확하게 확인하세요. . . . .

버전 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;
  }
}

응용 프로그램/뷰/헬퍼

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:

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

추가 콘텐츠에 사용되며 initView에 addHelperPath를 추가하고 애플리케이션을 로드하도록 변경할 수 있습니다. ini 파일 구성 항목을 통해 경로를 구성합니다. 다음과 같이

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
 protected function _initDoctype() {
 $this->bootstrap ( &#39;view&#39; );
 $view = $this->getResource ( &#39;view&#39; );
 $view->doctype ( &#39;XHTML1_STRICT&#39; );
 }
 protected function _initView() {
 $view = new Zend_View ();
 $view->setEncoding ( &#39;UTF-8&#39; );
 $view->doctype ( &#39;XHTML1_STRICT&#39; );
 $options = $this->getOptions ();
 $viewOptions = $options [&#39;resources&#39;][&#39;view&#39;][&#39;helperPath&#39;];
 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 = 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

이 글이 PHP 프로그래밍에 종사하는 모든 분들께 도움이 되기를 바랍니다.

Zend Framework의 맞춤형 Helper 클래스에 대한 더 많은 글과 주의사항 요약은 PHP 중국어 홈페이지를 참고해주세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.