這篇文章主要為大家展示了“Zend Framework自定義Helper類相關(guān)注意事項(xiàng)有哪些”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Zend Framework自定義Helper類相關(guān)注意事項(xiàng)有哪些”這篇文章吧。

具體如下:
編寫自定義的Helper類
編寫自定義的Helper類很容易,只要遵循以下幾個(gè)原則即可:
① 類名必須是 Zend_View_Helper_*,*是helper的名稱。例如,你在寫一個(gè)名為“specialPurpose”的類,類名將至少是"SpecialPurpose",另外你還應(yīng)該給類名加上前綴,建議將“View_Helper”作為前綴的一部份:“My_View_Helper_SpecialPurpose”。(注意大小寫)你將需要將前綴(不包含下劃線)傳遞給addHelperPath() 或 setHelperPath()。
② 類中必須有一個(gè)public的方法,該方法名與helper類名相同。這個(gè)方法將在你的模板調(diào)用"$this->specialPurpose()"時(shí)執(zhí)行。在我們的“specialPurpose”例子中,相應(yīng)的方法聲明可以是 “public function specialPurpose()”。
③ 一般來說,Helper類不應(yīng)該echo或print或有其它形式的輸出。它只需要返回值就可以了。返回的數(shù)據(jù)應(yīng)當(dāng)被轉(zhuǎn)義。
④ 類文件的命名應(yīng)該是helper方法的名稱,比如在"specialPurpose"例子中,文件要存為“SpecialPurpose.php”。
把helper類的文件放在你的helper路徑下, Zend_View就會(huì)自動(dòng)加載,實(shí)例化,持久化,并執(zhí)行。
三點(diǎn)類文件名稱,類名稱,類中helper方法,保持某種程度上的一致。
貼代碼:
兩個(gè)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() ?>
附加內(nèi)容,在initView中添加addHelperPath,可以改成采用加載application。ini文件配置項(xiàng)的方式把路徑進(jìn)行配置。如下
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 Framework自定義Helper類相關(guān)注意事項(xiàng)有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前標(biāo)題:ZendFramework自定義Helper類相關(guān)注意事項(xiàng)有哪些-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://www.chinadenli.net/article24/dodjje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、域名注冊、面包屑導(dǎo)航、關(guān)鍵詞優(yōu)化、服務(wù)器托管、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容