本文讲述了Zend Framework自定义Helper类相关注意事项。分享给大家供大家参考,具体如下:
编写自定义的Helper类
编写自定义的Helper类很容易,只要遵循以下几个原则即可:
① 类名必须是 Zend_View_Helper_*,*是helper的名称。例如,你在写一个名为“specialPurpose”的类,类名将至少是"SpecialPurpose",另外你还应该给类名加上前缀,建议将“View_Helper”作为前缀的一部份:“My_View_Helper_SpecialPurpose”。(注意大小写)你将需要将前缀(不包含下划线)传递给addHelperPath() 或 setHelperPath()。
② 类中必须有一个public的方法,该方法名与helper类名相同。这个方法将在你的模板调用"$this->specialPurpose()"时执行。在我们的“specialPurpose”例子中,相应的方法声明可以是 “public function specialPurpose()”。
③ 一般来说,Helper类不应该echo或print或有其它形式的输出。它只需要返回值就可以了。返回的数据应当被转义。
④ 类文件的命名应该是helper方法的名称,比如在"specialPurpose"例子中,文件要存为“SpecialPurpose.php”。
把helper类的文件放在你的helper路径下, Zend_View就会自动加载,实例化,持久化,并执行。
三点类文件名称,类名称,类中helper方法,保持某种程度上的一致。
贴代码:
两个helper,看清楚了,他们的不同啊。。。。。
version zf 1.10
Bootstrap.php
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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:
|
1
2
3
4
5
6
7
|
class Zend_View_Helper_Img extends Zend_View_Helper_Abstract
{
public function img()
{
return "this is a img";
}
}
|
TestHelper.php:
|
1
2
3
4
5
6
7
|
class My_View_Helper_TestHelper extends Zend_View_Helper_Abstract
{
public function testHelper()
{
return "this is a TestHelper";
}
}
|
action中使用:
|
1
2
3
|
<?php echo $this->doctype() ?>
<?php echo $this->img() ?>
<?php echo $this->testHelper() ?>
|
附加内容,在initView中添加addHelperPath,可以改成采用加载application。ini文件配置项的方式把路径进行配置。如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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;
}
}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[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程序设计有所帮助。
相关文章
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 63
-
2025-05-27 44
-
2025-05-27 95
-
2025-06-04 34
-
2025-06-04 71

