Zend Framework1,有没有办法覆盖每个控制器的Zend_View_Helper_*函数


Zend Framework1, Is there a way to override Zend_View_Helper_* functions per Controller?

我有一个如下所示的视图助手,它工作正常:

class Zend_View_Helper_List extends Zend_View_Helper_Abstract
{
    public function list()
    {
        return '<ul><li>Something Really Good</li></ul>'; // generated html 
    }
}

我的全球布局myapp.phtml中有这个:

<div><?php echo $this->list(); ?></div>

我的问题是,如何覆盖不同控制器甚至更精细的每个控制器操作中的list()

我尝试在每个控制器中设置一个视图变量,例如 $this->view->type,然后将其传递给列表函数<div><?php echo $this->list($this->type); ?></div>,但它看起来很脏而且不对!

您可以将

助手放在特定控制器的view/helpers文件夹中,以便仅对此控制器可见。

如果需要按操作更改帮助程序$this->view->setHelperPath('MyScripts/View/Helper/','MyScripts_View_Helper');,还可以为帮助程序添加新路径。

如果您只想使用单个视图帮助程序,则可以有效地使用变量。
你可以尝试这样的事情:

在您的 foo 操作中:

$this->view->type = 'action_foo';

在视图助手中:

public function list()
{
    if (isset($this->view->type)){
        if ('action_foo' == $this->view->type)
            return '<ul><li>Something Really Good for Foo Action</li></ul>'; // generated html 
        else
            return '<ul><li>' . $this->view->type . '</li></ul>'; // generated html 
    }
    else
        return '<ul><li>Something Really Good</li></ul>'; // generated html 
}