The Response ObjectUsageThe 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): To send the response output, including headers, use 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:
By doing this, all headers get sent at once, just prior to displaying the content.
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.
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:
Manipulating HeadersAs stated previously, one aspect of the response object's duties is to collect and emit HTTP response headers. A variety of methods exist for this:
In addition to the above methods, there are accessors for setting and retrieving the HTTP response code for the current request, setHttpResponseCode() and getHttpResponseCode(). Setting Cookie HeadersYou can inject HTTP Set-Cookie headers into the response object of an action controller by using the provided header class Zend_Http_Header_SetCookie Constructor ArgumentsThe following table lists all constructor arguments of Zend_Http_Header_SetCookie in the order they are accepted. All arguments are optional, but name and value must be supplied via their setters if not passed in via the constructor or the resulting Set-Cookie header be invalid.
Example #1 Populate Zend_Http_Header_SetCookie via constructor and add to response
Example #2 Populate Zend_Http_Header_SetCookie via setters and add to response
Named SegmentsThe 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:
In the above example, a call to /my/foo will cause the final body content of the response object to have the following structure:
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:
Testing for Exceptions in the Response ObjectAs mentioned earlier, by default, exceptions caught during dispatch are registered with the response object. Exceptions are registered in a stack, which allows you to keep all exceptions thrown -- application exceptions, dispatch exceptions, plugin exceptions, etc. Should you wish to check for particular exceptions or to log exceptions, you'll want to use the response object's exception API:
Subclassing the Response ObjectThe purpose of the response object is to collect headers and content from the various actions and plugins and return them to the client; secondarily, it also collects any errors (exceptions) that occur in order to process them, return them, or hide them from the end user. The base response class is Zend_Controller_Response_Abstract, and any subclass you create should extend that class or one of its derivatives. The various methods available have been listed in the previous sections. Reasons to subclass the response object include modifying how output is returned based on the request environment (e.g., not sending headers for CLI or PHP-GTK requests), adding functionality to return a final view based on content stored in named segments, etc.
|
|