2

How can I overwrite the default view object in zend framework so I could have the custom one?

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    
    function _initViewHelpers() { 
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('HTML4_STRICT');
        $view->setHelperPath(APPLICATION_PATH . '/helpers', '');        
        $view->headMeta()->appendHttpEquiv('Content-type', 'text/html;charset=utf-8')
                         ->appendName('description', 'Zend Framework');
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle('Zend Custom View');
        $view->setScriptPath(APPLICATION_PATH . '/themes/admin');
        
        return $view;
    }
}

The default view contains default script path for module. I want one path for all modules, to enable template system. The setScriptPath method should overwrite the default path generated by the view object, but it doesn't.

array(2) { [0]=> string(66) "C:/xampp/htdocs/NEOBBS_v6/application/modules/admin/views\scripts/" [1]=> string(51) "C:\xampp\htdocs\NEOBBS_v6\application/themes/admin/" }

it has two scriptPaths. Can this be done by overwriting the default view object?

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
brian
  • 21
  • 2

2 Answers2

3

What ArneRie posted is correct, however the ViewRenderer checks to see whether the standard script path is set and adds it if not. Since the paths are checked LIFO, what's happening is that the ViewRenderer is adding the standard path after your one and then always using that one.

What worked for me was to set both the standard path and my custom path at the same time, with the custom one being last, something like:

$view->setScriptPath(array(
    APPLICATION_PATH . '/views/scripts/', // or whatever the standard path is
    APPLICATION_PATH . '/themes/admin'
));

there may be a better solution for this though.

Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
  • yeah the code looks not elegant but it quite useful right now, thx tim – brian May 20 '11 at 09:59
  • This answer helped me fix a problem with overriding the default view helpers which I couldn't seem to do. I ended up adding the default zend view helper paths to my application.ini before my custom view helper paths and it did the trick! Cheers :) – chrismacp Jan 03 '12 at 16:01
2

Try to add:

        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
        $viewRenderer->setView($view);
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143