简体   繁体   中英

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:

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 :

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

My question is how can I override list() in different controller or even more granular, each controller action?

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> <div><?php echo $this->list($this->type); ?></div> , but it looks dirty and not right!

You can put your helpers in a view/helpers folder of specific Controller so it will be visible for this controller only.

You can also add new path for Helpers $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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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