PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Accessing the Layout Object

On occasion, you may need direct access to the layout object. There are three ways you can do this:

  • Within view scripts: use the layout() view helper, which returns the Zend_Layout instance registered with the front controller plugin.


    <?php $layout $this->layout(); ?>

    Since it returns the layout instance, you can also simply call methods on it, rather than assigning it to a variable.

  • Within action controllers: use the layout() action helper, which acts just like the view helper.

    // Calling helper as a method of the helper broker:
    $layout = $this->_helper->layout();
    
    // Or, more verbosely:
    $helper = $this->_helper->getHelper('Layout');
    $layout = $helper->getLayoutInstance();
    

    As with the view helper, since the action helper returns the layout instance, you can also simply call methods on it, rather than assigning it to a variable.

  • Elsewhere: use the static method getMvcInstance(). This will return the layout instance registered by the bootstrap resource.

    $layout = Zend_Layout::getMvcInstance();
    
  • Via the bootstrap: retrieve the layout resource, which will be the Zend_Layout instance.

    $layout = $bootstrap->getResource('Layout');
    

    Anywhere you have access to the bootstrap object, this method is preferred over using the static getMvcInstance() method.

Zend Framework