zend 1视图辅助函数在另一个视图助手类中使用

时间:2015-01-19 16:54:25

标签: php zend-framework helper

我有两个辅助类

链接是:

C:\ XAMPP \ htdocs中\ ECOM \应用\视图\助手\ comman.php

C:\ XAMPP \ htdocs中\ ECOM \应用\视图\助手\ 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

    } }

如何在另一个辅助类函数中使用一个辅助类函数?

2 个答案:

答案 0 :(得分:1)

您可以根据自己的情况使用以下技巧。

从您的getPageContent()文件中调用view帮助程序时,将view object作为param传递给$pageId view object并使用View在helper定义中调用另一个帮助器。

<?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}}

答案 1 :(得分:1)

我通常会做以下事情:

内部helper1

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

如果helper1正在接受一些参数,我将其修改为不带参数并返回。尝试一下,可能会有效。