Request

Type
Class
Namespace
craft\web\twig\variables
Inherits
craft\web\twig\variables\Request
Since
3.0
Deprecated in
in 3.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.

Signature

public string getClientOs ( )

getCookie()

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

View source

Arguments

Returns

yii\web\Cookie, null

Signature

public yii\web\Cookie, null getCookie ( \craft\web\twig\variables\string $name )

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.

Signature

public string getCsrfToken ( )

getFirstSegment()

Returns the first URI segment.

View source

Returns

string, null

Signature

public string, null getFirstSegment ( )

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

Signature

public string getHostInfo ( )

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 – The IP address.

Signature

public string getIpAddress ( )

getLastSegment()

Returns the last URL segment.

View source

Returns

string, null

Signature

public string, null getLastSegment ( )

getPageNum()

Returns the page number if this is a paginated request.

View source

Returns

integer

Signature

public integer getPageNum ( )

getParam()

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

View source

Arguments

Returns

mixed

Signature

public mixed getParam ( \craft\web\twig\variables\string $name, \craft\web\twig\variables\string $default = null )

getPath()

Returns the request's URI.

View source

Returns

mixed

Signature

public mixed getPath ( )

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

Signature

public string getPathInfo ( )

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

Signature

public integer getPort ( )

getPost()

Returns a value from post data.

View source

Arguments

Returns

mixed

Signature

public mixed getPost ( \craft\web\twig\variables\string $name = null, \craft\web\twig\variables\string $default = null )

getQuery()

Returns a variable from the query string.

View source

Arguments

Returns

mixed

Signature

public mixed getQuery ( \craft\web\twig\variables\string $name = null, \craft\web\twig\variables\string $default = null )

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.

Signature

public string getQueryString ( )

getQueryStringWithoutPath()

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

View source

Returns

string – The query string.

Signature

public string getQueryStringWithoutPath ( )

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

Signature

public string getRequestUri ( )

getScriptName()

Returns the script name used to access Craft.

View source

Returns

string

Signature

public string getScriptName ( )

getScriptUrl()

Returns the relative URL of the entry script.

View source

Returns

string

Signature

public string getScriptUrl ( )

getSegment()

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

View source

Arguments

Returns

string, null

Signature

public string, null getSegment ( \craft\web\twig\variables\int $num )

getSegments()

Returns all URI segments.

View source

Returns

array

Signature

public array getSegments ( )

getServerName()

Returns the server name.

View source

Returns

string

Signature

public string getServerName ( )

getServerPort()

Returns the server port number.

View source

Returns

integer, null

Signature

public integer, null getServerPort ( )

getUrl()

Returns the request's full URL.

View source

Returns

mixed

Signature

public mixed getUrl ( )

getUrlFormat()

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

View source

Returns

string

Signature

public string getUrlFormat ( )

getUrlReferrer()

Returns the URL referrer or null if not present.

View source

Returns

string

Signature

public string getUrlReferrer ( )

getUserAgent()

Returns the user agent or null if not present.

View source

Returns

string

Signature

public string getUserAgent ( )

getUserHost()

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

View source

Returns

string, null

Signature

public string, null getUserHost ( )

getUserHostAddress()

Returns the user IP address.

View source

Returns

string

Signature

public string getUserHostAddress ( )

isAjax()

Returns whether this is an Ajax request.

View source

Returns

boolean

Signature

public boolean isAjax ( )

isDelete()

Returns whether this is a DELETE request.

View source

Returns

boolean – Whether this is a DELETE request

Signature

public boolean isDelete ( )

isGet()

Returns whether this is a GET request.

View source

Returns

boolean – Whether this is a GET request

Signature

public boolean isGet ( )

isLivePreview()

Returns whether this is a Live Preview request.

View source

Returns

boolean

Signature

public boolean isLivePreview ( )

isMobileBrowser()

Returns whether the request is coming from a mobile browser.

View source

Arguments

Returns

boolean

Signature

public boolean isMobileBrowser ( \craft\web\twig\variables\bool $detectTablets = false )

isPost()

Returns whether this is a POST request.

View source

Returns

boolean – Whether this is a POST request

Signature

public boolean isPost ( )

isPut()

Returns whether this is a PUT request.

View source

Returns

boolean – Whether this is a PUT request

Signature

public boolean isPut ( )

isSecure()

Returns whether this is a secure connection.

View source

Returns

boolean

Signature

public boolean isSecure ( )