Class THttpResponse

Description

Implements interfaces:

THttpResponse class

THttpResponse implements the mechanism for sending output to client users.

To output a string to client, use write(). By default, the output is buffered until flush() is called or the application ends. The output in the buffer can also be cleaned by clear(). To disable output buffering, set BufferOutput property to false.

To send cookies to client, use getCookies(). To redirect client browser to a new URL, use redirect(). To send a file to client, use writeFile().

By default, THttpResponse is registered with TApplication as the response module. It can be accessed via TApplication::getResponse().

THttpResponse may be configured in application configuration file as follows

<module id="response" class="System.Web.THttpResponse" CacheExpire="20" CacheControl="nocache" BufferOutput="true" />

where CacheExpire, CacheControl and BufferOutput are optional properties of THttpResponse.

THttpResponse sends charset header if either Charset or TGlobalization.Charset is set.

Since 3.1.2, HTTP status code can be set with the StatusCode property.

Note: Some HTTP Status codes can require additional header or body information. So, if you use StatusCode in your application, be sure to add theses informations. E.g : to make an http authentication :

  1. public function clickAuth ($sender, $param)
  2. {
  3. $response=$this->getResponse();
  4. $response->setStatusCode(401);
  5. $response->appendHeader('WWW-Authenticate: Basic realm="Test"');
  6. }

This event handler will sent the 401 status code (Unauthorized) to the browser, with the WWW-Authenticate header field. This will force the browser to ask for a username and a password.

  • since: 3.0
  • version: $Id: THttpResponse.php 2445 2008-04-21 13:45:24Z tof $
  • author: Qiang Xue <qiang.xue@gmail.com>

Located in /Web/THttpResponse.php (line 67)

TComponent
   |
   --TApplicationComponent
      |
      --TModule
         |
         --THttpResponse
Method Summary
void __destruct ()
void addCookie (THttpCookie $cookie)
void appendHeader (string $value)
void appendLog (string $message, integer $messageType, [string $destination = ''], [string $extraHeaders = ''])
void clear ()
void createHtmlWriter ([string $type = null])
void createNewHtmlWriter (string $type, ITextWriter $writer)
void flush ()
void flushContent ()
boolean getBufferOutput ()
string getCacheControl ()
integer getCacheExpire ()
string getCharset ()
string getContents ()
string getContentType ()
boolean getHasAdapter ()
string getHtmlWriterType ()
integer getStatusCode ()
void getStatusReason (string 0)
void httpRedirect (string $url)
void init (TXmlElement $config)
void redirect (string $url)
void reload ()
void removeCookie (THttpCookie $cookie)
void sendHttpHeader ()
void setAdapter (THttpResponseAdapter $adapter)
void setBufferOutput (boolean $value)
void setCacheControl (string $value)
void setCacheExpire (integer $value)
void setCharset (string $charset)
string setContentType (mixed $type)
void setHtmlWriterType (string $value)
void setStatusCode (integer $status, [mixed $reason = null])
void write (string $str)
void writeFile (string $fileName, [string $content = null], [string $mimeType = null], [array $headers = null])
Methods
Destructor __destruct (line 121)

Destructor.

Flushes any existing content in buffer.

  • access: public
void __destruct ()
addCookie (line 506)

Sends a cookie.

Do not call this method directly. Operate with the result of getCookies instead.

  • access: public
void addCookie (THttpCookie $cookie)
appendHeader (line 481)

Sends a header.

  • access: public
void appendHeader (string $value)
  • string $value: header
appendLog (line 496)

Writes a log message into error log.

This method is simple wrapper of PHP function error_log.

void appendLog (string $message, integer $messageType, [string $destination = ''], [string $extraHeaders = ''])
  • string $message: The error message that should be logged
  • integer $messageType: where the error should go
  • string $destination: The destination. Its meaning depends on the message parameter as described above
  • string $extraHeaders: The extra headers. It's used when the message parameter is set to 1. This message type uses the same internal function as mail() does.
clear (line 470)

Clears any existing buffered content.

  • access: public
void clear ()
createHtmlWriter (line 549)

Creates a new instance of HTML writer.

If the type of the HTML writer is not supplied, HtmlWriterType will be assumed.

  • access: public
void createHtmlWriter ([string $type = null])
  • string $type: type of the HTML writer to be created. If null, HtmlWriterType will be assumed.
createNewHtmlWriter (line 565)

Create a new html writer instance.

This method is used internally. Please use createHtmlWriter instead.

  • access: public
void createNewHtmlWriter (string $type, ITextWriter $writer)
  • string $type: type of HTML writer to be created.
  • ITextWriter $writer: text writer holding the contents.
flush (line 409)

Flush the response contents and headers.

  • access: public
void flush ()
flushContent (line 421)

Outputs the buffered content, sends content-type and charset header.

This method is used internally. Please use flush instead.

  • access: public
void flushContent ()
getAdapter (line 138)
  • return: response adapter, null if not exist.
  • access: public
THttpResponseAdapter getAdapter ()
getBufferOutput (line 233)
  • return: whether to enable output buffer
  • access: public
boolean getBufferOutput ()
getCacheControl (line 184)
  • return: cache control method to use for session pages
  • access: public
string getCacheControl ()
getCacheExpire (line 168)
  • return: time-to-live for cached session pages in minutes, this has no effect for nocache limiter. Defaults to 180.
  • access: public
integer getCacheExpire ()
getCharset (line 217)
  • return: output charset.
  • access: public
string getCharset ()
getContents (line 461)

Returns the content in the output buffer.

The buffer will NOT be cleared after calling this method. Use clear() is you want to clear the buffer.

  • return: output that is in the buffer.
  • access: public
string getContents ()
getContentType (line 209)
  • return: current content type
  • access: public
string getContentType ()
getCookies (line 291)
  • return: list of output cookies
  • access: public
THttpCookieCollection getCookies ()
getHasAdapter (line 146)
  • return: true if adapter exists, false otherwise.
  • access: public
boolean getHasAdapter ()
getHtmlWriterType (line 531)
  • return: the type of HTML writer to be used, defaults to THtmlWriter
  • access: public
string getHtmlWriterType ()
getStatusCode (line 253)
  • return: HTTP status code, defaults to 200
  • access: public
integer getStatusCode ()
getStatusReason (line 284)
  • access: public
void getStatusReason (string 0)
  • string 0: HTTP status reason
httpRedirect (line 386)

Redirect the browser to another URL and exists the current application.

This method is used internally. Please use redirect instead.

  • access: public
void httpRedirect (string $url)
  • string $url: URL to be redirected to. If the URL is a relative one, the base URL of the current request will be inserted at the beginning.
init (line 157)

Initializes the module.

This method is required by IModule and is invoked by application. It starts output buffer if it is enabled.

  • access: public
void init (TXmlElement $config)

Redefinition of:
TModule::init()
Initializes the module.
redirect (line 372)

Redirects the browser to the specified URL.

The current application will be terminated after this method is invoked.

  • access: public
void redirect (string $url)
  • string $url: URL to be redirected to. If the URL is a relative one, the base URL of the current request will be inserted at the beginning.
reload (line 401)

Reloads the current page.

The effect of this method call is the same as user pressing the refresh button on his browser (without post data).

  • access: public
void reload ()
removeCookie (line 523)

Deletes a cookie.

Do not call this method directly. Operate with the result of getCookies instead.

  • access: public
void removeCookie (THttpCookie $cookie)
sendContentTypeHeader (line 441)

Sends content type header if charset is not empty.

  • access: protected
void sendContentTypeHeader ()
sendHttpHeader (line 433)

Send the HTTP header with the status code (defaults to 200) and status reason (defaults to OK)

  • access: protected
void sendHttpHeader ()
setAdapter (line 130)
  • access: public
void setAdapter (THttpResponseAdapter $adapter)
setBufferOutput (line 242)
  • access: public
  • throws: TInvalidOperationException if session is started already
void setBufferOutput (boolean $value)
  • boolean $value: whether to enable output buffer
setCacheControl (line 193)
  • access: public
void setCacheControl (string $value)
  • string $value: cache control method to use for session pages. Valid values include none/nocache/private/private_no_expire/public
setCacheExpire (line 176)
  • access: public
void setCacheExpire (integer $value)
  • integer $value: time-to-live for cached session pages in minutes, this has no effect for nocache limiter.
setCharset (line 225)
  • access: public
void setCharset (string $charset)
  • string $charset: output charset.
setContentType (line 201)
  • return: content type, default is text/html
  • access: public
string setContentType (mixed $type)
setHtmlWriterType (line 539)
  • access: public
void setHtmlWriterType (string $value)
  • string $value: the type of HTML writer to be used, may be the class name or the namespace
setStatusCode (line 263)

Set the HTTP status code for the response.

  • access: public
void setStatusCode (integer $status, [mixed $reason = null])
  • integer $status: HTTP status code
write (line 303)

Outputs a string.

It may not be sent back to user immediately if output buffer is enabled.

  • access: public
void write (string $str)
  • string $str: string to be output
writeFile (line 317)

Sends a file back to user.

Make sure not to output anything else after calling this method.

  • access: public
  • throws: TInvalidDataValueException if the file cannot be found
void writeFile (string $fileName, [string $content = null], [string $mimeType = null], [array $headers = null])
  • string $fileName: file name
  • string $content: content to be set. If null, the content will be read from the server file pointed to by $fileName.
  • string $mimeType: mime type of the content.
  • array $headers: list of headers to be sent. Each array element represents a header string (e.g. 'Content-Type: text/plain').

Inherited Methods

Inherited From TModule

TModule::getID()
TModule::init()
TModule::setID()

Inherited From TApplicationComponent

TApplicationComponent::getApplication()
TApplicationComponent::getRequest()
TApplicationComponent::getResponse()
TApplicationComponent::getService()
TApplicationComponent::getSession()
TApplicationComponent::getUser()
TApplicationComponent::publishAsset()
TApplicationComponent::publishFilePath()

Inherited From TComponent

TComponent::addParsedObject()
TComponent::attachEventHandler()
TComponent::canGetProperty()
TComponent::canSetProperty()
TComponent::createdOnTemplate()
TComponent::detachEventHandler()
TComponent::evaluateExpression()
TComponent::evaluateStatements()
TComponent::getEventHandlers()
TComponent::getSubProperty()
TComponent::hasEvent()
TComponent::hasEventHandler()
TComponent::hasProperty()
TComponent::raiseEvent()
TComponent::setSubProperty()
TComponent::__get()
TComponent::__set()

Documentation generated on Mon, 21 Apr 2008 11:35:10 -0400 by phpDocumentor 1.3.0RC4