Class THttpResponse
THttpResponse class
THttpResponse implements the mechanism for sending output to client users.
To output a string to client, use THttpResponse::write()
. By default, the output is
buffered until THttpResponse::flush()
is called or the application ends. The output in
the buffer can also be cleaned by THttpResponse::clear()
. To disable output buffering,
set BufferOutput property to false.
To send cookies to client, use THttpResponse::getCookies()
. To redirect client
browser to a new URL, use THttpResponse::redirect()
. To send a file to client, use
THttpResponse::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 getCacheExpire CacheExpire, getCacheControl CacheControl and getBufferOutput BufferOutput are optional properties of THttpResponse.
THttpResponse sends charset header if either setCharset() Charset or TGlobalization::setCharset() TGlobalization.Charset is set.
Since 3.1.2, HTTP status code can be set with the setStatusCode StatusCode property.
Note: Some HTTP Status codes can require additional header or body information. So, if you use setStatusCode 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.
- TComponent
-
TApplicationComponent
-
TModule implements IModule
-
THttpResponse implements ITextWriter
Copyright: Copyright © 2005-2014 PradoSoft
License: http://www.pradosoft.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 3.0
Located at Web/THttpResponse.php
public
|
|
public
|
|
public
|
|
public
boolean
|
|
public
|
#
init(
Initializes the module. This method is required by IModule and is invoked by application. It starts output buffer if it is enabled. |
public
integer
|
|
public
|
|
public
string
|
|
public
|
|
public
string
|
|
public
string
|
|
public
string|boolean
|
|
public
|
|
public
boolean
|
|
public
|
|
public
integer
|
|
public
|
#
setStatusCode( integer $status, string $reason = null )
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 |
public
|
|
public
|
|
public
|
#
write( string $str )
Outputs a string. It may not be sent back to user immediately if output buffer is enabled. |
public
|
#
writeFile( string $fileName, string $content = null, string $mimeType = null, array $headers = null, boolean $forceDownload = true, string $clientFileName = null, integer $fileSize = null )
Sends a file back to user. Make sure not to output anything else after calling this method. |
public
|
#
redirect( string $url )
Redirects the browser to the specified URL. The current application will be terminated after this method is invoked. |
public
|
#
httpRedirect( string $url )
Redirect the browser to another URL and exists the current application. This
method is used internally. Please use |
public
|
#
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). |
public
|
|
public
|
|
public
|
#
flushContent( boolean $continueBuffering = true )
Outputs the buffered content, sends content-type and charset header. This
method is used internally. Please use |
protected
|
#
ensureHttpHeaderSent( )
Ensures that the HTTP header with the status code and status reason are sent |
protected
|
#
sendHttpHeader( )
Send the HTTP header with the status code (defaults to 200) and status reason (defaults to OK) |
protected
|
#
ensureContentTypeHeaderSent( )
Ensures that the HTTP header with the status code and status reason are sent |
protected
|
|
public
string
|
#
getContents( )
Returns the content in the output buffer. The buffer will NOT be cleared
after calling this method. Use |
public
|
|
public
array
|
|
public
|
|
public
|
#
appendLog( string $message, integer $messageType = 0, string $destination = '', string $extraHeaders = '' )
Writes a log message into error log. This method is simple wrapper of PHP function error_log. |
public
|
#
addCookie(
Sends a cookie. Do not call this method directly. Operate with the result of
|
public
|
#
removeCookie(
Deletes a cookie. Do not call this method directly. Operate with the result
of |
public
string
|
|
public
|
|
public
|
#
createHtmlWriter( string $type = null )
Creates a new instance of HTML writer. If the type of the HTML writer is not supplied, getHtmlWriterType HtmlWriterType will be assumed. |
public
|
#
createNewHtmlWriter( string $type,
Create a new html writer instance. This method is used internally. Please use
|
getID(),
setID()
|
getApplication(),
getRequest(),
getResponse(),
getService(),
getSession(),
getUser(),
publishAsset(),
publishFilePath()
|
string |
DEFAULT_CONTENTTYPE |
'text/html' |
|
string |
DEFAULT_CHARSET |
'UTF-8' |
GLOBAL_RAISE_EVENT_LISTENER
|