我有两个辅助类
链接是:
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
传递(如 $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 正在接受一些参数,我将其修改为不接受参数并返回。试试看,可能会起作用。