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

Usage

The response object is the logical counterpart to the request object. Its purpose is to collate content and/or headers so that they may be returned en masse. Additionally, the front controller will pass any caught exceptions to the response object, allowing the developer to gracefully handle exceptions. This functionality may be overridden by setting Zend_Controller_Front::throwExceptions(true):

<?php
$front
->throwExceptions(true);

To send the response output, including headers, use sendResponse().

<?php
$response
->sendResponse();

Note

By default, the front controller calls sendResponse() when it has finished dispatching the request; typically you will never need to call it. However, if you wish to manipulate the response or use it in testing, you can override this behaviour by setting the returnResponse flag with Zend_Controller_Front::returnResponse(true):

<?php
$front
->returnResponse(true);
$response $front->dispatch();

// do some more processing, such as logging...
// and then send the output:
$response->sendResponse();

Developers should make use of the response object in their action controllers. Instead of directly rendering output and sending headers, push them to the response object:

<?php
// Within an action controller action:
// Set a header
$this->getResponse()
    ->
setHeader('Content-Type''text/html')
    ->
appendBody($content);

By doing this, all headers get sent at once, just prior to displaying the content.

Note

If using the action controller view integration, you do not need to set the rendered view script content in the response object, as Zend_Controller_Action::render() does this by default.

Should an exception occur in an application, check the response object's isException() flag, and retrieve the exception using getException(). Additionally, one may create custom response objects that redirect to error pages, log exception messages, do pretty formatting of exception messages (for development environments), etc.

You may retrieve the response object following the front controller dispatch(), or request the front controller to return the response object instead of rendering output.

<?php
// retrieve post-dispatch:
$front->dispatch();
$response $front->getResponse();
if (
$response->isException()) {
    
// log, mail, etc...
}

// Or, have the front controller dispatch() process return it
$front->returnResponse(true);
$response $front->dispatch();

// do some processing...

// finally, echo the response
$response->sendResponse();

By default, exception messages are not displayed. This behaviour may be overridden by calling renderExceptions(), or enabling the front controller to throwExceptions(), as shown above:

<?php
$response
->renderExceptions(true);
$front->dispatch($request$response);

// or:
$front->returnResponse(true);
$response $front->dispatch();
$response->renderExceptions();
$response->sendResponse();

// or:
$front->throwExceptions(true);
$front->dispatch();

Zend Framework