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 :
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.
Method Summary |
void
|
Sends a cookie.
|
void
|
Sends a header.
|
void
|
appendLog
( string $message, integer $messageType, string $destination, string $extraHeaders)
Writes a log message into error log.
|
void
|
Clears any existing buffered content.
|
void
|
Creates a new instance of HTML writer.
|
void
|
Create a new html writer instance.
|
void
|
Flush the response contents and headers.
|
void
|
Outputs the buffered content, sends content-type and charset header.
|
THttpResponseAdapter
|
|
boolean
|
|
string
|
|
integer
|
|
string
|
|
string
|
Returns the content in the output buffer.
|
string
|
|
THttpCookieCollection
|
|
boolean
|
|
string
|
|
integer
|
|
void
|
|
void
|
Redirect the browser to another URL and exists the current application.
|
void
|
Initializes the module.
|
void
|
Redirects the browser to the specified URL.
|
void
|
Reloads the current page.
|
void
|
Deletes a cookie.
|
protected
void
|
Sends content type header if charset is not empty.
|
protected
void
|
Send the HTTP header with the status code (defaults to 200) and status reason (defaults to OK)
|
void
|
|
void
|
|
void
|
|
void
|
|
void
|
|
string
|
|
void
|
|
void
|
Set the HTTP status code for the response.
|
void
|
Outputs a string.
|
void
|
writeFile
( string $fileName, string $content, string $mimeType, array $headers, boolean $forceDownload, string $clientFileName, integer $fileSize)
Sends a file back to user.
|
Method Details |
addCookie
Sends a cookie.
Do not call this method directly. Operate with the result of getCookies instead.
Input |
THttpCookie | $cookie | cook to be sent |
Output |
Exception |
|
appendHeader
public void appendHeader |
(string $value ) |
Sends a header.
Input |
string | $value | header |
Output |
Exception |
|
appendLog
public void appendLog |
(string $message , integer $messageType , string $destination , string $extraHeaders ) |
Writes a log message into error log.
This method is simple wrapper of PHP function error_log.
Input |
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. |
Output |
Exception |
|
clear
Clears any existing buffered content.
|
createHtmlWriter
public void createHtmlWriter |
(string $type ) |
Creates a new instance of HTML writer.
If the type of the HTML writer is not supplied, HtmlWriterType will be assumed.
Input |
string | $type | type of the HTML writer to be created. If null, HtmlWriterType will be assumed. |
Output |
Exception |
|
createNewHtmlWriter
public void createNewHtmlWriter |
(string $type , ITextWriter $writer ) |
Create a new html writer instance.
This method is used internally. Please use createHtmlWriter instead.
Input |
string | $type | type of HTML writer to be created. |
ITextWriter | $writer | text writer holding the contents. |
Output |
Exception |
|
flush
Flush the response contents and headers.
|
flushContent
public void flushContent |
() |
Outputs the buffered content, sends content-type and charset header.
This method is used internally. Please use flush instead.
|
getAdapter
|
getBufferOutput
public boolean getBufferOutput |
() |
Output |
boolean
| whether to enable output buffer |
Exception |
|
getCacheControl
public string getCacheControl |
() |
Output |
string
| cache control method to use for session pages |
Exception |
|
getCacheExpire
public integer getCacheExpire |
() |
Output |
integer
| time-to-live for cached session pages in minutes, this has no effect for nocache limiter. Defaults to 180. |
Exception |
|
getCharset
public string getCharset |
() |
Output |
string
| output charset. |
Exception |
|
getContents
public string getContents |
() |
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.
Output |
string
| output that is in the buffer. |
Exception |
|
getContentType
public string getContentType |
() |
Output |
string
| current content type |
Exception |
|
getCookies
|
getHasAdapter
public boolean getHasAdapter |
() |
Output |
boolean
| true if adapter exists, false otherwise. |
Exception |
|
getHtmlWriterType
public string getHtmlWriterType |
() |
Output |
string
| the type of HTML writer to be used, defaults to THtmlWriter |
Exception |
|
getStatusCode
public integer getStatusCode |
() |
Output |
integer
| HTTP status code, defaults to 200 |
Exception |
|
getStatusReason
public void getStatusReason |
(string 0 ) |
Input |
string | 0 | HTTP status reason |
Output |
Exception |
|
httpRedirect
public void httpRedirect |
(string $url ) |
Redirect the browser to another URL and exists the current application.
This method is used internally. Please use redirect instead.
Input |
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. |
Output |
Exception |
|
init
Initializes the module.
This method is required by IModule and is invoked by application. It starts output buffer if it is enabled.
Input |
TXmlElement | $config | module configuration |
Output |
Exception |
|
redirect
public void redirect |
(string $url ) |
Redirects the browser to the specified URL.
The current application will be terminated after this method is invoked.
Input |
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. |
Output |
Exception |
|
reload
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).
|
removeCookie
Deletes a cookie.
Do not call this method directly. Operate with the result of getCookies instead.
Input |
THttpCookie | $cookie | cook to be deleted |
Output |
Exception |
|
sendContentTypeHeader
protected void sendContentTypeHeader |
() |
Sends content type header if charset is not empty.
|
sendHttpHeader
protected void sendHttpHeader |
() |
Send the HTTP header with the status code (defaults to 200) and status reason (defaults to OK)
|
setAdapter
public void setAdapter |
(THttpResponseAdapter $adapter ) |
Input |
THttpResponseAdapter | $adapter | response adapter |
Output |
Exception |
|
setBufferOutput
public void setBufferOutput |
(boolean $value ) |
Input |
boolean | $value | whether to enable output buffer |
Output |
Exception |
throws | TInvalidOperationException if session is started already |
|
setCacheControl
public void setCacheControl |
(string $value ) |
Input |
string | $value | cache control method to use for session pages. Valid values include none/nocache/private/private_no_expire/public |
Output |
Exception |
|
setCacheExpire
public void setCacheExpire |
(integer $value ) |
Input |
integer | $value | time-to-live for cached session pages in minutes, this has no effect for nocache limiter. |
Output |
Exception |
|
setCharset
public void setCharset |
(string $charset ) |
Input |
string | $charset | output charset. |
Output |
Exception |
|
setContentType
public string setContentType |
(mixed $type ) |
Input |
mixed | $type | |
Output |
string
| content type, default is text/html |
Exception |
|
setHtmlWriterType
public void setHtmlWriterType |
(string $value ) |
Input |
string | $value | the type of HTML writer to be used, may be the class name or the namespace |
Output |
Exception |
|
setStatusCode
public void setStatusCode |
(integer $status , string $reason ) |
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
Input |
integer | $status | HTTP status code |
string | $reason | HTTP status reason, defaults to standard HTTP reasons |
Output |
Exception |
|
write
public void write |
(string $str ) |
Outputs a string.
It may not be sent back to user immediately if output buffer is enabled.
Input |
string | $str | string to be output |
Output |
Exception |
|
writeFile
public void writeFile |
(string $fileName , string $content , string $mimeType , array $headers , boolean $forceDownload , string $clientFileName , integer $fileSize ) |
Sends a file back to user.
Make sure not to output anything else after calling this method.
Input |
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. |
Output |
Exception |
throws | TInvalidDataValueException if the file cannot be found |
|