简体   繁体   English

Zend Framework1,是否有一种方法可以覆盖每个Controller的Zend_View_Helper_ *函数?

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

I have a View Helper like below and it is working perfectly fine: 我有一个如下所示的View Helper,它工作得很好:

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

I have this in my global layout myapp.phtml : 我在全局布局myapp.phtml有这个:

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

My question is how can I override list() in different controller or even more granular, each controller action? 我的问题是,如何在不同的控制器中或更详细地覆盖每个控制器操作中的list()

I have tried to set a View variable in each controller eg $this->view->type and then pass it to the list function <div><?php echo $this->list($this->type); ?></div> 我尝试在每个控制器中设置一个View变量,例如$this->view->type ,然后将其传递给列表函数<div><?php echo $this->list($this->type); ?></div> <div><?php echo $this->list($this->type); ?></div> , but it looks dirty and not right! <div><?php echo $this->list($this->type); ?></div> ,但看起来很脏,不对!

You can put your helpers in a view/helpers folder of specific Controller so it will be visible for this controller only. 您可以将您的助手放置在特定Controller的view/helpers文件夹中,以便仅对该控制器可见。

You can also add new path for Helpers $this->view->setHelperPath('MyScripts/View/Helper/','MyScripts_View_Helper'); 您还可以为Helpers $this->view->setHelperPath('MyScripts/View/Helper/','MyScripts_View_Helper');添加新路径$this->view->setHelperPath('MyScripts/View/Helper/','MyScripts_View_Helper'); if you need to change it per Action. 如果您需要根据操作进行更改。

If you only want to have a single view helper you can effectively use variables. 如果只希望有一个视图助手,则可以有效地使用变量。
You can try something like this: 您可以尝试如下操作:

In your foo action: 在您的foo动作中:

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

In your View Helper: 在您的视图助手中:

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 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM