Example Analysis of Zend_Controller_Response encapsulated by response object of Zend Framework
This article mainly introduces the example analysis of Zend Framework's response object encapsulation Zend_Controller_Response, which is very detailed and has a certain reference value. Interested friends must read it!
The details are as follows:
Overview
The response object is logically the partner of the request object. The goal is to collect message bodies and / or headers, so it is possible to return a large number of results.
Basic implementation of Zend_Controller_Response response object
├── Response
│ ├── Abstract.php
│ ├── Cli.php
│ ├── Exception.php
│ ├── Http.php
│ └── HttpTestCase.php
Zend_Controller_Response_Abstract
Abstract class Zend_Controller_Response_Abstract {/ * Body content * @ var array * / protected $_ body = array (); / * * Exception stack * @ var Exception * / protected $_ exceptions = array (); / * Array of headers. Each header is an array with keys' name' and 'value' * @ var array * / protected $_ headers = array (); / * Array of raw headers. Each header is a single string, the entire header to emit * @ var array * / protected $_ headersRaw = array (); / * HTTP response code to use in headers * @ var int * / protected $_ httpResponseCode = 200; / * Flag; is this response a redirect? * @ var boolean * / protected $_ isRedirect = false; / * * Whether or not to render exceptions; off by default * @ var boolean * / protected $renderExceptions = false; / * Flag If true, when header operations are called after headers have been * sent, an exception will be raised; otherwise, processing will continue * as normal. Defaults to true. * * @ see canSendHeaders () * @ var boolean * / public $headersSentThrowsException = true; / * * Normalize a header name * * Normalizes a header name to X-Capitalized-Names * * @ param string $name * @ return string * / protected function _ normalizeHeader ($name) {$filtered = str_replace (array ('-','_'),', (string) $name); $filtered = ucwords (strtolower ($filtered)) $filtered = str_replace (','-', $filtered); return $filtered;} / * * Set a header * * If $replace is true, replaces any headers already defined with that * $name. * * @ param string $name * @ param string $value * @ param boolean $replace * @ return Zend_Controller_Response_Abstract * / public function setHeader ($name, $value, $replace = false) {$this- > canSendHeaders (true); $name = $this- > _ normalizeHeader ($name); $value = (string) $value; if ($replace) {foreach ($this- > _ headers as $key = > $header) {if ($name = $header ['name']) {unset ($this- > _ headers [$key]) }} $this- > _ headers [] = array ('name' = > $name,' value' = > $value, 'replace' = > $replace); return $this;} / * * Set redirect URL * * Sets Location header and response code. Forces replacement of any prior * redirects. * * @ param string $url * @ param int $code * @ return Zend_Controller_Response_Abstract * / public function setRedirect ($url, $code = 302) {$this- > canSendHeaders (true); $this- > setHeader ('Location', $url, true)-> setHttpResponseCode ($code); return $this;} / * * Is this a redirect? * @ return boolean * / public function isRedirect () {return $this- > _ isRedirect;} / * * Return array of headers See {@ link $_ headers} for format * * @ return array * / public function getHeaders () {return $this- > _ headers;} / * * Clear headers * * @ return Zend_Controller_Response_Abstract * / public function clearHeaders () {$this- > _ headers = array (); return $this } / * * Clears the specified HTTP header * * @ param string $name * @ return Zend_Controller_Response_Abstract * / public function clearHeader ($name) {if (! Count ($this- > _ headers) {return $this;} foreach ($this- > _ headers as $index = > $header) {if ($name = = $header ['name']) {unset ($this- > _ headers [$index]);} return $this } / * * Set raw HTTP header * * Allows setting non key = > value headers, such as status codes * * @ param string $value * @ return Zend_Controller_Response_Abstract * / public function setRawHeader ($value) {$this- > canSendHeaders (true); if ('Location' = = substr ($value, 0,8)) {$this- > _ isRedirect = true;} $this- > _ headersRaw [] = (string) $value; return $this } / * Retrieve all {@ link setRawHeader () raw HTTP headers} * * @ return array * / public function getRawHeaders () {return $this- > _ headersRaw;} / * * Clear all {@ link setRawHeader () raw HTTP headers} * * @ return Zend_Controller_Response_Abstract * / public function clearRawHeaders () {$this- > _ headersRaw = array (); return $this } / * * Clears the specified raw HTTP header * * @ param string $headerRaw * @ return Zend_Controller_Response_Abstract * / public function clearRawHeader ($headerRaw) {if (! Count ($this- > _ headersRaw) {return $this;} $key = array_search ($headerRaw, $this- > _ headersRaw); if ($key! = = false) {unset ($this- > _ headersRaw [$key]);} return $this;} / * * Clear all headers, normal and raw * * @ return Zend_Controller_Response_Abstract * / public function clearAllHeaders () {return $this- > clearHeaders ()-> clearRawHeaders () } / * Set HTTP response code to use with headers * * @ param int $code * @ return Zend_Controller_Response_Abstract * / public function setHttpResponseCode ($code) {if (! is_int ($code) | | (100 > $code) | |
< $code)) { require_once 'Zend/Controller/Response/Exception.php'; throw new Zend_Controller_Response_Exception('Invalid HTTP response code'); } if ((300 = $code)) { $this->