本文讲述了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
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
更多关于zend相关内容感兴趣的读者可查看本站专题:《zend framework框架入门教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《thinkphp入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:
本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!