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

Named Segments

The response object has support for "named segments". This allows you to segregate body content into different segments and order those segments so output is returned in a specific order. Internally, body content is saved as an array, and the various accessor methods can be used to indicate placement and names within that array.

As an example, you could use a preDispatch() hook to add a header to the response object, then have the action controller add body content, and a postDispatch() hook add a footer:

<?php
// Assume that this plugin class is registered with the front controller
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
    public function 
preDispatch(Zend_Controller_Request_Abstract $request)
    {
        
$response $this->getResponse();
        
$view = new Zend_View();
        
$view->setBasePath('../views/scripts');

        
$response->prepend('header'$view->render('header.phtml'));
    }

    public function 
postDispatch(Zend_Controller_Request_Abstract $request)
    {
        
$response $this->getResponse();
        
$view = new Zend_View();
        
$view->setBasePath('../views/scripts');

        
$response->append('footer'$view->render('footer.phtml'));
    }
}

// a sample action controller
class MyController extends Zend_Controller_Action
{
    public function 
fooAction()
    {
        
$this->render();
    }
}

In the above example, a call to /my/foo will cause the final body content of the response object to have the following structure:

<?php
array(
    
'header'  => ..., // header content
    
'default' => ..., // body content from MyController::fooAction()
    
'footer'  => ...  // footer content
);

When this is rendered, it will render in the order in which elements are arranged in the array.

A variety of methods can be used to manipulate the named segments:

  • setBody() allows you to pass a second value, $name, indicating a named segment. If you provide a segment name it will overwrite that specific named segment or create it if it does not exist (appending to the body array by default). If no named segment is passed to setBody(), it resets the entire body content array.

  • appendBody() also allows you to pass a second value, $name, indicating a named segment. If you provide a segment name it will append the supplied content to the existing content in the named segment, or create the segment if it does not exist (appending to the body array by default). If no named segment is passed to appendBody(), it will append the supplied content to the named segment 'default', creating it if it does not already exist.

  • prepend($name, $content) will create a segment named $name and place it at the beginning of the array. If the segment exists already, it will be removed prior to the operation (i.e., overwritten and replaced).

  • append($name, $content) will create a segment named $name and place it at the end of the array. If the segment exists already, it will be removed prior to the operation (i.e., overwritten and replaced).

  • insert($name, $content, $parent = null, $before = false) will create a segment named $name. If provided with a $parent segment, the new segment will be placed either before or after that segment (based on the value of $before) in the array. If the segment exists already, it will be removed prior to the operation (i.e., overwritten and replaced).

  • clearBody($name = null) will remove a single named segment if a $name is provided (and the entire array otherwise).

  • getBody($spec = false) can be used to retrieve a single array segment if $spec is the name of a named segment. If $spec is FALSE, it returns a string formed by concatenating all named segments in order. If $spec is TRUE, it returns the body content array.

Zend Framework