## 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 2740 2009-11-08 09:46:55Z godzilla80@gmx.net $
- author: Qiang Xue <qiang.xue@gmail.com>

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

    TComponent
       |
       --TApplicationComponent
          |
          --TModule
             |
             --THttpResponse

Class Constant Summary
DEFAULT_CHARSET = 'UTF-8'
DEFAULT_CONTENTTYPE = 'text/html'
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|boolean 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|boolean $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], [boolean $forceDownload = true], [string $clientFileName = null], [integer $fileSize = null])
Methods
Destructor __destruct (line 124)

Destructor.

Flushes any existing content in buffer.

- access: public

void __destruct ()
addCookie (line 540)

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

Sends a header.

- access: public

void appendHeader (string $value)

- string $value: header

appendLog (line 530)

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

Clears any existing buffered content.

- access: public

void clear ()
createHtmlWriter (line 583)

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

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

Flush the response contents and headers.

- access: public

void flush ()
flushContent (line 450)

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

- return: response adapter, null if not exist.
- access: public

THttpResponseAdapter getAdapter ()
getBufferOutput (line 236)

- return: whether to enable output buffer
- access: public

boolean getBufferOutput ()
getCacheControl (line 187)

- return: cache control method to use for session pages
- access: public

string getCacheControl ()
getCacheExpire (line 171)

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

- return: output charset.
- access: public

string|boolean getCharset ()
getContents (line 495)

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

- return: current content type
- access: public

string getContentType ()
getCookies (line 297)

- return: list of output cookies
- access: public

THttpCookieCollection getCookies ()
getHasAdapter (line 149)

- return: true if adapter exists, false otherwise.
- access: public

boolean getHasAdapter ()
getHtmlWriterType (line 565)

- return: the type of HTML writer to be used, defaults to THtmlWriter
- access: public

string getHtmlWriterType ()
getStatusCode (line 256)

- return: HTTP status code, defaults to 200
- access: public

integer getStatusCode ()
getStatusReason (line 290)

- access: public

void getStatusReason (string 0)

- string 0: HTTP status reason

httpRedirect (line 410)

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

This method is used internally. Please use redirect instead.

- access: public
- since: 3.1.5 You can set the set StatusCode to a value between 300 and 399 before calling this function to change the type of redirection. If not specified, StatusCode will be 302 (Found) by default

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

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

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

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

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

Sends content type header with optional charset.

- access: protected

void sendContentTypeHeader ()
sendHttpHeader (line 462)

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

- access: protected

void sendHttpHeader ()
setAdapter (line 133)

- access: public

void setAdapter (THttpResponseAdapter $adapter)

- THttpResponseAdapter $adapter: response adapter

setBufferOutput (line 245)

- access: public
- throws: TInvalidOperationException if session is started already

void setBufferOutput (boolean $value)

- boolean $value: whether to enable output buffer

setCacheControl (line 196)

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

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

- access: public

void setCharset (string|boolean $charset)

- string|boolean $charset: output charset.

setContentType (line 204)

- return: content type, default is text/html
- access: public

string setContentType (mixed $type)
setHtmlWriterType (line 573)

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

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

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

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], [boolean $forceDownload = true], [string $clientFileName = null], [integer $fileSize = 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').
- boolean $forceDownload: force download of file, even if browser able to display inline. Defaults to 'true'.
- string $clientFileName: force a specific file name on client side. Defaults to 'null' means auto-detect.
- integer $fileSize: size of file or content in bytes if already known. Defaults to 'null' means auto-detect.

#### 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()

Class Constants
DEFAULT_CHARSET = 'UTF-8' (line 70)
DEFAULT_CONTENTTYPE = 'text/html' (line 69)

Documentation generated on Mon, 22 Feb 2010 12:17:53 +0000 by phpDocumentor 1.3.0RC4