Request

Type
Class
Namespace
craft\web\twig\variables
Inherits
craft\web\twig\variables\Request
Since
3.0.0
Deprecated in
in 3.0.0

Request functions.

View source (opens new window)

# Public Methods

Method Description
getClientOs() Returns whether the client is running "Windows", "Mac", "Linux" or "Other", based on the browser's UserAgent string.
getCookie() Returns a yii\web\Cookie (opens new window) if it exists, otherwise, null.
getCsrfToken() Returns the random token used to perform CSRF validation.
getFirstSegment() Returns the first URI segment.
getHostInfo() Returns the schema and host part of the application URL. The returned URL does not have an ending slash. By default this is determined based on the user request information.
getIpAddress() Retrieves the best guess of the client’s actual IP address taking into account numerous HTTP proxy headers due to variations in how different ISPs handle IP addresses in headers between hops.
getLastSegment() Returns the last URL segment.
getPageNum() Returns the page number if this is a paginated request.
getParam() Returns a variable from either the query string or the post data.
getPath() Returns the request's URI.
getPathInfo() Returns the path info of the currently requested URL. This refers to the part that is after the entry script and before the question mark. The starting and ending slashes are stripped off.
getPort() Returns the port to use for insecure requests. Defaults to 80, or the port specified by the server if the current request is insecure.
getPost() Returns a value from post data.
getQuery() Returns a variable from the query string.
getQueryString() Returns part of the request URL that is after the question mark.
getQueryStringWithoutPath() Returns the request’s query string, without the p= parameter.
getRequestUri() Returns the request URI portion for the currently requested URL. This refers to the portion that is after the host info part. It includes the query string part if any.
getScriptName() Returns the script name used to access Craft.
getScriptUrl() Returns the relative URL of the entry script.
getSegment() Returns a specific URI segment, or null if the segment doesn't exist.
getSegments() Returns all URI segments.
getServerName() Returns the server name.
getServerPort() Returns the server port number.
getUrl() Returns the request's full URL.
getUrlFormat() Returns which URL format we're using (PATH_INFO or the query string)
getUrlReferrer() Returns the URL referrer or null if not present.
getUserAgent() Returns the user agent or null if not present.
getUserHost() Returns the user host name or null if it cannot be determined.
getUserHostAddress() Returns the user IP address.
isAjax() Returns whether this is an Ajax request.
isDelete() Returns whether this is a DELETE request.
isGet() Returns whether this is a GET request.
isLivePreview() Returns whether this is a Live Preview request.
isMobileBrowser() Returns whether the request is coming from a mobile browser.
isPost() Returns whether this is a POST request.
isPut() Returns whether this is a PUT request.
isSecure() Returns whether this is a secure connection.

# getClientOs()

Returns whether the client is running "Windows", "Mac", "Linux" or "Other", based on the browser's UserAgent string.

View source (opens new window)

Returns

string (opens new window) – The OS the client is running.

# getCookie()

Returns a yii\web\Cookie (opens new window) if it exists, otherwise, null.

View source (opens new window)

Arguments

Returns

yii\web\Cookie (opens new window), null (opens new window)

# getCsrfToken()

Returns the random token used to perform CSRF validation.

The token will be read from cookie first. If not found, a new token will be generated.

View source (opens new window)

Returns

string (opens new window) – The random token for CSRF validation.

# getFirstSegment()

Returns the first URI segment.

View source (opens new window)

Returns

string (opens new window), null (opens new window)

# getHostInfo()

Returns the schema and host part of the application URL. The returned URL does not have an ending slash. By default this is determined based on the user request information.

View source (opens new window)

Returns

string (opens new window), null (opens new window)

# getIpAddress()

Retrieves the best guess of the client’s actual IP address taking into account numerous HTTP proxy headers due to variations in how different ISPs handle IP addresses in headers between hops.

Considering any of these server vars besides REMOTE_ADDR can be spoofed, this method should not be used when you need a trusted source for the IP address. Use $_SERVER['REMOTE_ADDR'] instead.

View source (opens new window)

Returns

string (opens new window), null (opens new window) – The IP address.

# getLastSegment()

Returns the last URL segment.

View source (opens new window)

Returns

string (opens new window), null (opens new window)

# getPageNum()

Returns the page number if this is a paginated request.

View source (opens new window)

Returns

integer (opens new window)

# getParam()

Returns a variable from either the query string or the post data.

View source (opens new window)

Arguments

Returns

mixed

# getPath()

Returns the request's URI.

View source (opens new window)

Returns

string (opens new window)

# getPathInfo()

Returns the path info of the currently requested URL. This refers to the part that is after the entry script and before the question mark. The starting and ending slashes are stripped off.

View source (opens new window)

Returns

string (opens new window)

# getPort()

Returns the port to use for insecure requests. Defaults to 80, or the port specified by the server if the current request is insecure.

View source (opens new window)

Returns

integer (opens new window)

# getPost()

Returns a value from post data.

View source (opens new window)

Arguments

Returns

mixed

# getQuery()

Returns a variable from the query string.

View source (opens new window)

Arguments

Returns

mixed

# getQueryString()

Returns part of the request URL that is after the question mark.

View source (opens new window)

Returns

string (opens new window) – The part of the request URL that is after the question mark.

# getQueryStringWithoutPath()

Returns the request’s query string, without the p= parameter.

View source (opens new window)

Returns

string (opens new window) – The query string.

# getRequestUri()

Returns the request URI portion for the currently requested URL. This refers to the portion that is after the host info part. It includes the query string part if any.

View source (opens new window)

Returns

string (opens new window)

# getScriptName()

Returns the script name used to access Craft.

View source (opens new window)

Returns

string (opens new window)

# getScriptUrl()

Returns the relative URL of the entry script.

View source (opens new window)

Returns

string (opens new window)

# getSegment()

Returns a specific URI segment, or null if the segment doesn't exist.

View source (opens new window)

Arguments

Returns

string (opens new window), null (opens new window)

# getSegments()

Returns all URI segments.

View source (opens new window)

Returns

array (opens new window)

# getServerName()

Returns the server name.

View source (opens new window)

Returns

string (opens new window), null (opens new window)

# getServerPort()

Returns the server port number.

View source (opens new window)

Returns

integer (opens new window), null (opens new window)

# getUrl()

Returns the request's full URL.

View source (opens new window)

Returns

string (opens new window)

# getUrlFormat()

Returns which URL format we're using (PATH_INFO or the query string)

View source (opens new window)

Returns

string (opens new window)

# getUrlReferrer()

Returns the URL referrer or null if not present.

View source (opens new window)

Returns

string (opens new window), null (opens new window)

# getUserAgent()

Returns the user agent or null if not present.

View source (opens new window)

Returns

string (opens new window), null (opens new window)

# getUserHost()

Returns the user host name or null if it cannot be determined.

View source (opens new window)

Returns

string (opens new window), null (opens new window)

# getUserHostAddress()

Returns the user IP address.

View source (opens new window)

Returns

string (opens new window), null (opens new window)

# isAjax()

Returns whether this is an Ajax request.

View source (opens new window)

Returns

boolean (opens new window)

# isDelete()

Returns whether this is a DELETE request.

View source (opens new window)

Returns

boolean (opens new window) – Whether this is a DELETE request

# isGet()

Returns whether this is a GET request.

View source (opens new window)

Returns

boolean (opens new window) – Whether this is a GET request

# isLivePreview()

Returns whether this is a Live Preview request.

View source (opens new window)

Returns

boolean (opens new window)

# isMobileBrowser()

Returns whether the request is coming from a mobile browser.

View source (opens new window)

Arguments

Returns

boolean (opens new window)

# isPost()

Returns whether this is a POST request.

View source (opens new window)

Returns

boolean (opens new window) – Whether this is a POST request

# isPut()

Returns whether this is a PUT request.

View source (opens new window)

Returns

boolean (opens new window) – Whether this is a PUT request

# isSecure()

Returns whether this is a secure connection.

View source (opens new window)

Returns

boolean (opens new window)