簡體   English   中英

zend 1視圖助手功能在另一個視圖助手類中使用

[英]zend 1 view helper functions use in another view helper class

我有兩個幫助班

鏈接是:

C:\\ xampp \\ htdocs \\ ecom \\ application \\ views \\ helpers \\ comman.php

C:\\ xampp \\ htdocs \\ ecom \\ application \\ views \\ helpers \\ RefineUrl.php

class Zend_View_Helper_Refinestr
{
    public function Refinestr($str, $options = array()){
     ..............
     .............
     return $str;
    }


}

第二個是

class Zend_View_Helper_Comman
{
    public function Comman(){
        return $this;
    }
    public function getPageContent($pageId){
        //  return $pageId;
        $mapper  = new Application_Model_StaticpageMapper();
        $selectedFields=array('desc');
        $tblName=array($mapper->getDbTable()->_name);
        $whr= "`id`=$pageId";
        $content=$mapper->fetchSelectedFields($tblName,$selectedFields,$whr);

        $des=$content[0]['desc'];
// here i want to use function Refinestr() of another helper class how i use this
$des=$this->Refinestr($des); 
// not working , searching this function inside comman class

    } }

如何在另一個幫助器類函數中使用一個幫助器類函數?

您可以根據情況使用以下技巧。

view文件中調用getPageContent()幫助程序時,將其中的view object作為param傳遞給param (例如$pageId ),並使用該view object在幫助程序定義中調用另一個幫助程序。

View文件:

<?php echo $this->getPageContent($pageId, $this); ?>

助手文件:

class Zend_View_Helper_GetPageContent {
    public function getPageContent($pageId, $viewObj) {
        // return $pageId;
        $mapper = new Application_Model_StaticpageMapper ();
        $selectedFields = array ('desc' 
        );
        $tblName = array ($mapper->getDbTable ()->_name 
        );
        $whr = "`id`=$pageId";
        $content = $mapper->fetchSelectedFields ( $tblName, $selectedFields, $whr );

        $des = $content [0] ['desc'];
        // here i want to use function Refinestr() of another helper class how i
        // use this
        $des = $viewObj->Refinestr($des); //use view object to call another helper
    }
}

另一個助手將保持原樣。

解決此問題的另一種方法是,在引導時在Zend Registry中設置視圖對象,並在幫助程序文件中使用該registry variable來調用另一個幫助程序。

Bootstrap文件中:

protected function _initConfig() {
    $this->bootstrap('view');
    $this->_view = $this->getResource('view');
    Zend_Registry::set('viewObj', $this->_view);
}

Helper文件:

class Zend_View_Helper_GetPageContent {
        public function getPageContent($pageId) {
            // return $pageId;
            $mapper = new Application_Model_StaticpageMapper ();
            $selectedFields = array ('desc');
            $tblName = array ($mapper->getDbTable ()->_name);
            $whr = "`id`=$pageId";
            $content = $mapper->fetchSelectedFields ( $tblName, $selectedFields, $whr );

            $des = $content [0] ['desc'];
            // here i want to use function Refinestr() of another helper class how i
            // use this
            $viewObj = Zend_Registry::get('viewObj');
            $des = $viewObj->Refinestr($des); //use view object to call another helper
        }
    }

我通常會執行以下操作:

內部助手1

$this->helper1()->view->helper2();

萬一helper1接受了一些參數,我將其修改為不接受任何參數而只返回。 試試看,可能會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM