Zend Framework自定义Helper类相关注意事项总结
本文讲述了ZendFramework自定义Helper类相关注意事项。分享给大家供大家参考,具体如下:
编写自定义的Helper类
编写自定义的Helper类很容易,只要遵循以下几个原则即可:
①类名必须是Zend_View_Helper_*,*是helper的名称。例如,你在写一个名为“specialPurpose”的类,类名将至少是"SpecialPurpose",另外你还应该给类名加上前缀,建议将“View_Helper”作为前缀的一部份:“My_View_Helper_SpecialPurpose”。(注意大小写)你将需要将前缀(不包含下划线)传递给addHelperPath()或setHelperPath()。
②类中必须有一个public的方法,该方法名与helper类名相同。这个方法将在你的模板调用"$this->specialPurpose()"时执行。在我们的“specialPurpose”例子中,相应的方法声明可以是“publicfunctionspecialPurpose()”。
③一般来说,Helper类不应该echo或print或有其它形式的输出。它只需要返回值就可以了。返回的数据应当被转义。
④类文件的命名应该是helper方法的名称,比如在"specialPurpose"例子中,文件要存为“SpecialPurpose.php”。
把helper类的文件放在你的helper路径下,Zend_View就会自动加载,实例化,持久化,并执行。
三点类文件名称,类名称,类中helper方法,保持某种程度上的一致。
贴代码:
两个helper,看清楚了,他们的不同啊。。。。。
version zf1.10
Bootstrap.php
classBootstrapextendsZend_Application_Bootstrap_Bootstrap{ protectedfunction_initDoctype(){ $this->bootstrap('view'); $view=$this->getResource('view'); $view->doctype('XHTML1_STRICT'); } protectedfunction_initView(){ $view=newZend_View(); $view->setEncoding('UTF-8'); $view->doctype('XHTML1_STRICT'); $view->addHelperPath('../application/views/helpers','My_View_Helper'); $viewRenderer=newZend_Controller_Action_Helper_ViewRenderer(); Zend_Controller_Action_HelperBroker::addHelper($viewRenderer); $viewRenderer->setView($view); return$view; } }
application/views/helpers
Img.php:
classZend_View_Helper_ImgextendsZend_View_Helper_Abstract { publicfunctionimg() { return"thisisaimg"; } }
TestHelper.php:
classMy_View_Helper_TestHelperextendsZend_View_Helper_Abstract { publicfunctiontestHelper() { return"thisisaTestHelper"; } }
action中使用:
<?phpecho$this->doctype()?> <?phpecho$this->img()?> <?phpecho$this->testHelper()?>
附加内容,在initView中添加addHelperPath,可以改成采用加载application。ini文件配置项的方式把路径进行配置。如下
classBootstrapextendsZend_Application_Bootstrap_Bootstrap{ protectedfunction_initDoctype(){ $this->bootstrap('view'); $view=$this->getResource('view'); $view->doctype('XHTML1_STRICT'); } protectedfunction_initView(){ $view=newZend_View(); $view->setEncoding('UTF-8'); $view->doctype('XHTML1_STRICT'); $options=$this->getOptions(); $viewOptions=$options['resources']['view']['helperPath']; if(is_array($viewOptions)){ foreach($viewOptionsas$helperName=>$path) { $view->addHelperPath($path,$helperName); } } $viewRenderer=newZend_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相关内容感兴趣的读者可查看本站专题:《ZendFrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。