Skip to content

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

Public Methods ​

MethodDescription
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 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

Returns ​

string – The OS the client is running.

getCookie() ​

Returns a yii\web\Cookie if it exists, otherwise, null.

View source

Arguments ​

Returns ​

yii\web\Cookie, null

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

Returns ​

string – The random token for CSRF validation.

getFirstSegment() ​

Returns the first URI segment.

View source

Returns ​

string, null

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

Returns ​

string, null

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

Returns ​

string, null – The IP address.

getLastSegment() ​

Returns the last URL segment.

View source

Returns ​

string, null

getPageNum() ​

Returns the page number if this is a paginated request.

View source

Returns ​

integer

getParam() ​

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

View source

Arguments ​

Returns ​

mixed

getPath() ​

Returns the request's URI.

View source

Returns ​

string

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

Returns ​

string

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

Returns ​

integer

getPost() ​

Returns a value from post data.

View source

Arguments ​

Returns ​

mixed

getQuery() ​

Returns a variable from the query string.

View source

Arguments ​

Returns ​

mixed

getQueryString() ​

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

View source

Returns ​

string – 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

Returns ​

string – 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

Returns ​

string

getScriptName() ​

Returns the script name used to access Craft.

View source

Returns ​

string

getScriptUrl() ​

Returns the relative URL of the entry script.

View source

Returns ​

string

getSegment() ​

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

View source

Arguments ​

Returns ​

string, null

getSegments() ​

Returns all URI segments.

View source

Returns ​

array

getServerName() ​

Returns the server name.

View source

Returns ​

string, null

getServerPort() ​

Returns the server port number.

View source

Returns ​

integer, null

getUrl() ​

Returns the request's full URL.

View source

Returns ​

string

getUrlFormat() ​

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

View source

Returns ​

string

getUrlReferrer() ​

Returns the URL referrer or null if not present.

View source

Returns ​

string, null

getUserAgent() ​

Returns the user agent or null if not present.

View source

Returns ​

string, null

getUserHost() ​

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

View source

Returns ​

string, null

getUserHostAddress() ​

Returns the user IP address.

View source

Returns ​

string, null

isAjax() ​

Returns whether this is an Ajax request.

View source

Returns ​

boolean

isDelete() ​

Returns whether this is a DELETE request.

View source

Returns ​

boolean – Whether this is a DELETE request

isGet() ​

Returns whether this is a GET request.

View source

Returns ​

boolean – Whether this is a GET request

isLivePreview() ​

Returns whether this is a Live Preview request.

View source

Returns ​

boolean

isMobileBrowser() ​

Returns whether the request is coming from a mobile browser.

View source

Arguments ​

Returns ​

boolean

isPost() ​

Returns whether this is a POST request.

View source

Returns ​

boolean – Whether this is a POST request

isPut() ​

Returns whether this is a PUT request.

View source

Returns ​

boolean – Whether this is a PUT request

isSecure() ​

Returns whether this is a secure connection.

View source

Returns ​

boolean