Search code examples
zend-frameworkzend-view

How to Extend Zend View Properly


In the view and layout scripts of my MVC application I regularly require access to convenience methods such as isLoggedIn() or isAdmin(). I would like to put these in a base view so that I can access them with $this->isLoggedIn() or $this->isAdmin(). What's the right way to do this?

I tried extending Zend_View with MY_Base_View. I then created a View resource and initialised it from my bootstrap. I know that its being initialised because the page title works fine. Here's the code from the resource:

$this->_view = new MY_Base_View();
$this->_view->headTitle('My page title!');

When I'm in the view script, however, if I print_r($this), it says the object is a Zend View object, (not MY_Base_View).

Have had similar problems with the layout to.

Thanks!


Solution

  • Ahh!! Sorted it out. In the view resource, I needed to set the view for the ViewRenderer, as follows:

        $viewRenderer = Zend_Controller_Action_HelperBroker
            ::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($this->_view);           
    

    (It's in the Resources section of the manual!) Anyhow, that's all I had to do. In order to do its work, the layout looks for the view registered with the renderer...