我想向ViewHelper中添加多个函数。通常有一个函数的名称与类和文件名相似。
如何将多个函数添加到一个ViewHelper中?
例如:
class Zend_View_Helper_MyMenuHelper extends Zend_View_Helper_Abstract
{
public function Horizontal($parameter)
{
return "...";
}
}echo $this->MyMenuHelper()->Horizontal($parameter);
发布于 2012-01-25 19:58:26
亚历克斯走在正确的道路上,但他的答案中遗漏了一些东西:实际的myMenuHelper()方法必须返回视图帮助器本身,才能正常工作:
class Zend_View_Helper_MyMenuHelper extends Zend_View_Helper_Abstract
{
public function myMenuHelper()
{
return $this;
}
public function horizontal() { ... }
// more methods...
}然后,如前所述:
echo $this->myMenuHelper()->horizontal();发布于 2012-01-28 22:49:22
有时您不希望通过视图助手的main方法,尽管这对于某些类型的逻辑来说并不是那么糟糕。在这种情况下,请使用getHelper()
class Zend_View_Helper_MyMenuHelper extends Zend_View_Helper_Abstract
{
public function myMenuHelper()
{
// some logic, maybe the main one
}
public function horizontal()
{
// some other logic
}
}以下示例完全绕过了myMenuHelper():
// in controller
$this->view->getHelper('MyMenuHelper')->horizontal();
// in view
$this->getHelper('MyMenuHelper')->horizontal();`例如,在某些情况下,我用控制器中的一些内部数据填充视图帮助器,直接在视图中调用它的main方法,该方法作用于这些数据。
// in controller
$this->view->getHelper('MyMenuHelper')->storeData($someArray);
// in view
$this->myMenuHelper(); // iterates over $someArray发布于 2011-12-09 03:52:27
请尝试以小写字母开头的函数名
class Zend_View_Helper_MyMenuHelper extends Zend_View_Helper_Abstract
{
public function horizontal($parameter)
{
return "...";
}
}在视图中:
echo $this->myMenuHelper()->horizontal($parameter);https://stackoverflow.com/questions/8436528
复制相似问题