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

Using Zend_Layout with the Zend Framework MVC

Zend_Controller offers a rich set of functionality for extension via its front controller plugins and action controller helpers. Zend_View also has helpers. Zend_Layout takes advantage of these various extension points when used with the MVC components.

Zend_Layout::startMvc() creates an instance of Zend_Layout with any optional configuration you provide it. It then registers a front controller plugin that renders the layout with any application content once the dispatch loop is done, and registers an action helper to allow access to the layout object from your action controllers. Additionally, you may at any time grab the layout instance from within a view script using the Layout view helper.

First, let's look at how to initialize Zend_Layout for use with the MVC:

<?php
// In your bootstrap:
Zend_Layout::startMvc();

startMvc() can take an optional array of options or Zend_Config object to customize the instance; these options are detailed in this section.

In an action controller, you may then access the layout instance as an action helper:

<?php
class FooController extends Zend_Controller_Action
{
    public function 
barAction()
    {
        
// disable layouts for this action:
        
$this->_helper->layout->disableLayout();
    }

    public function 
bazAction()
    {
        
// use different layout script with this action:
        
$this->_helper->layout->setLayout('foobaz');
    };
}

In your view scripts, you can then access the layout object via the Layout view helper. This view helper is slightly different than others in that it takes no arguments, and returns an object instead of a string value. This allows you to immediately call methods on the layout object:


<?php $this->layout()->setLayout('foo'); // set alternate layout ?>

At any time, you can fetch the Zend_Layout instance registered with the MVC via the getMvcInstance() static method:

<?php
// Returns null if startMvc() has not first been called
$layout Zend_Layout::getMvcInstance();

Finally, Zend_Layout's front controller plugin has one important feature in addition to rendering the layout: it retrieves all named segments from the response object and assigns them as layout variables, assigning the 'default' segment to the variable 'content'. This allows you to access your application content and render it in your view scripts.

As an example, let's say your code first hits FooController::indexAction(), which renders some content to the default response segment, and then forwards to NavController::menuAction(), which renders content to the 'nav' response segment. Finally, you forward to CommentController::fetchAction() and fetch some comments, but render those to the default response segment as well (which appends content to that segment). Your view script could then render each separately:


<body>
    <!-- renders /nav/menu -->
    <div id="nav"><?php echo $this->layout()->nav ?></div>

    <!-- renders /foo/index + /comment/fetch -->
    <div id="content"><?php echo $this->layout()->content ?></div>
</body>

This feature is particularly useful when used in conjunction with the ActionStack action helper and plugin, which you can use to setup a stack of actions through which to loop, and thus create widgetized pages.

Zend Framework