## Class THttpResponse

Description

Implements interfaces:

- ITextWriter

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 :

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

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 2541 2008-10-21 15:05:13Z qiang.xue $
- 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 ()
THttpResponseAdapter getAdapter ()
boolean getBufferOutput ()
string getCacheControl ()
integer getCacheExpire ()
string getCharset ()
string getContents ()
string getContentType ()
THttpCookieCollection getCookies ()
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 sendContentTypeHeader ()
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, [string $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 512)

Sends a cookie.

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

- access: public

void addCookie (THttpCookie $cookie)

- THttpCookie $cookie: cook to be sent

appendHeader (line 487)

Sends a header.

- access: public

void appendHeader (string $value)

- string $value: header

appendLog (line 502)

Writes a log message into error log.

This method is simple wrapper of PHP function error_log.

- access: public
- see: http://us2.php.net/manual/en/function.error-log.php

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 476)

Clears any existing buffered content.

- access: public

void clear ()
createHtmlWriter (line 555)

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 571)

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 412)

Flush the response contents and headers.

- access: public

void flush ()
flushContent (line 424)

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 467)

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 294)

- 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 537)

- 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 287)

- access: public

void getStatusReason (string 0)

- string 0: HTTP status reason

httpRedirect (line 389)

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)

- TXmlElement $config: module configuration

Redefinition of:

TModule::init()
  Initializes the module.

redirect (line 375)

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 404)

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 529)

Deletes a cookie.

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

- access: public

void removeCookie (THttpCookie $cookie)

- THttpCookie $cookie: cook to be deleted

sendContentTypeHeader (line 447)

Sends content type header if charset is not empty.

- access: protected

void sendContentTypeHeader ()
sendHttpHeader (line 436)

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)

- THttpResponseAdapter $adapter: response 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 545)

- 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 266)

Set the HTTP status code for the response.

The code and its reason will be sent to client using the currently requested http protocol version (see THttpRequest::getHttpProtocolVersion) Keep in mind that HTTP/1.0 clients might not understand all status codes from HTTP/1.1

- access: public

void setStatusCode (integer $status, [string $reason = null])

- integer $status: HTTP status code
- string $reason: HTTP status reason, defaults to standard HTTP reasons

write (line 306)

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 320)

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 Sun, 11 Jan 2009 20:22:41 -0500 by phpDocumentor 1.3.0RC4