views:

175

answers:

1

I want to modify a view from Action Helper in Zend Framework in preDispatch() method. So I do something like:

class MyHelper extends Zend_Controller_Action_Helper_Abstract {
    public function preDispatch() {
        $view = $this->getActionController()->view;
        $view->doSomething();
    }
}

Is it OK to do so? What I want to do is - MyHelper needs to adjust some paths to templates (in this case view is a SmartyView) according to users locale, so I would like to read out users locale in my action helper and then adjust view accordingly.

I am doing right here or should I go some different way?

Thanks!

+1  A: 

You could do it with an ActionHelper. But you would have to call it explicitly then. The preDispatch callback method does not exist in an Action Helper. If you want to use the dispatch callbacks, you are looking for a Zend Controller Plugin.

An alternative to your approach would be to init Zend_Locale in your bootstrap, before initializing Zend_View. You could then fetch the locale when initializing Zend_View and adjust paths directly during bootstrap, instead of during the dispatch cycle. See the examples on Zend_Application for an idea.

Gordon
I did use Zend_Locale in bootstrap before, but the path adjustments depends not only on locale, but could also depend let's say on request parameters, so doing this in bootstrap is probably not the best way to do it... I'll try going with controller plugins, thanks!
Laimoncijus
How do I access view object from Zend Controller Plugin? Somehow I cannot see it anywhere on plugin object...
Laimoncijus
@Laimoncijus You could do `Zend_Layout::getMvcInstance()->getView()`
Gordon