Zend Framework 自定义 Helper 类编写规范与配置详解
本文介绍了在 Zend Framework 框架中创建自定义 Helper 类时需要关注的关键事项,旨在为开发者提供参考与借鉴。
如何构建自定义的 Helper 类
开发自定义 Helper 类相对简便,只需严格遵守以下核心准则:
① 类名必须严格遵循 "Zend_View_Helper_*" 格式,其中星号代表具体的 Helper 名称。例如,若创建一个名为 “specialPurpose” 的功能类,其基础类名至少应为 "SpecialPurpose"。为了区分第三方或项目特有组件,建议添加前缀,如使用 “My_View_Helper_SpecialPurpose”。请注意大小写敏感性,且在调用 addHelperPath() 或 setHelperPath() 注册路径时,需传入不包含下划线的前缀部分(即 My)。\n\n② 类内部必须包含一个 public 访问权限的方法,且该方法名称需与 Helper 的核心标识(即去掉前缀后的类名部分)保持一致。当模板中执行 "$this->specialPurpose()" 调用时,将触发此方法。以 “specialPurpose” 为例,对应的方法声明应写作 “public function specialPurpose()”。\n\n③ 最佳实践要求 Helper 类不应直接使用 echo、print 或产生其他形式的屏幕输出。该类的主要职责是处理逻辑并返回数据,返回的内容应当经过适当的转义处理以确保安全。\n\n④ 类文件的文件名应与 Helper 方法的名称相对应。在上述 “specialPurpose” 案例中,对应的源文件应保存为 “SpecialPurpose.php”。
将 Helper 类的源文件放置于指定的 Helper 路径后,Zend_View 组件会自动完成该类的加载、实例化、持久化存储以及执行操作。
需保持类文件名、类名称以及类内部的 Helper 方法名三者之间的一致性。
具体代码实现如下:
此处展示两个不同的 Helper 示例,请仔细辨别它们之间的差异。
测试环境版本为 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 控制器中的调用方式:
<?php echo $this->doctype() ?>
<?php echo $this->img() ?>
<?php echo $this->testHelper() ?>
补充说明:除了在 initView 中硬编码添加 addHelperPath 外,还可以采用从 application.ini 配置文件读取路径的方式来实现动态配置。具体配置示例如下所示。
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 = 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 类的经验分享能对大家有所帮助。