Craft 2 Class Reference

Class HttpRequestService

Package
craft.app.services
Namespace
Craft
Inheritance
class HttpRequestService » \CHttpRequest » \CApplicationComponent » \CComponent
Implements
\IApplicationComponent
Since
1.0
Source Code
craft/app/services/HttpRequestService.php

HttpRequestService provides APIs for getting information about the current HTTP request.

An instance of HttpRequestService is globally accessible in Craft via craft()->request.

Public Properties
Property Type Description Defined By
acceptTypes string Returns user browser accept types, null if not present. \CHttpRequest
actionSegments array / null Returns an array of the action path segments, if this is an action request. HttpRequestService
baseUrl string Returns the relative URL for the application. \CHttpRequest
behaviors array the behaviors that should be attached to this component. \CApplicationComponent
browser array Returns information about the capabilities of user browser. \CHttpRequest
browserLanguages array / false Returns a list of languages the user has selected in their browser’s settings, canonicalized using LocaleData::getCanonicalID. HttpRequestService
clientOs string Returns whether the client is running "Windows", "Mac", "Linux" or "Other", based on the browser's UserAgent string. HttpRequestService
contentType string Returns request content-type \CHttpRequest
cookies CookieCollection Returns the cookie collection. The result can be used like an associative array. Adding HttpCookie objects to the collection will send the cookies to the client; and removing the objects from the collection will delete those cookies on the client. HttpRequestService
csrfCookie array the property values (in name-value pairs) used to initialize the CSRF cookie. \CHttpRequest
csrfToken string Gets the current CSRF token from the CSRF token cookie, (re)creating the cookie if it is missing or invalid. HttpRequestService
csrfTokenName string the name of the token used to prevent CSRF. \CHttpRequest
enableCookieValidation bool whether cookies should be validated to ensure they are not tampered. \CHttpRequest
enableCsrfValidation bool whether to enable CSRF (Cross-Site Request Forgery) validation. \CHttpRequest
hostInfo string Returns the schema and host part of the application URL. \CHttpRequest
hostName string Returns the host name, without “http://” or “https://”. HttpRequestService
httpVersion string Returns the version of the HTTP protocol used by client. \CHttpRequest
ipAddress string 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. HttpRequestService
isAjaxRequest bool Returns whether this is an AJAX (XMLHttpRequest) request. \CHttpRequest
isDeleteRequest bool Returns whether this is a DELETE request. \CHttpRequest
isFlashRequest bool Returns whether this is an Adobe Flash or Adobe Flex request. \CHttpRequest
isGetRequest bool Returns whether this is a GET request. HttpRequestService
isInitialized bool Checks if this application component has been initialized. \CApplicationComponent
isPatchRequest bool Returns whether this is a PATCH request. \CHttpRequest
isPostRequest bool Returns whether this is a POST request. \CHttpRequest
isPutRequest bool Returns whether this is a PUT request. \CHttpRequest
isSecureConnection bool Return if the request is sent via secure channel (https). \CHttpRequest
jsonAsArray bool whether the parsing of JSON REST requests should return associative arrays for object data. \CHttpRequest
mimeType string Returns the MIME type that is going to be included in the response via the Content-Type header. HttpRequestService
normalizedPath string Returns the path Craft should use to route this request, including the CP trigger if it is in there. HttpRequestService
pageNum int Returns the current page number. HttpRequestService
path string Returns the request’s Craft path. HttpRequestService
pathInfo string Returns the path info of the currently requested URL. \CHttpRequest
port integer Returns the port to use for insecure requests. \CHttpRequest
post mixed Returns a POST parameter, or all of them. HttpRequestService
preferredAcceptType array Returns the user preferred accept MIME type. \CHttpRequest
preferredAcceptTypes array Returns an array of user accepted MIME types in order of preference. \CHttpRequest
preferredLanguage string Returns the user-preferred language that should be used by this application. \CHttpRequest
preferredLanguages array Returns an array of user accepted languages in order of preference. \CHttpRequest
query mixed Returns a query string parameter, or all of them. HttpRequestService
queryString string Returns part of the request URL that is after the question mark. \CHttpRequest
queryStringWithoutPath string Returns the request’s query string, without the p= parameter. HttpRequestService
rawBody string Returns the raw HTTP request body. \CHttpRequest
requestType string Returns the request type, such as GET, POST, HEAD, PUT, PATCH, DELETE. \CHttpRequest
requestUri string Returns the request URI portion for the currently requested URL. \CHttpRequest
restParams array Returns request parameters. Typically PUT, PATCH or DELETE. \CHttpRequest
scriptFile string Returns entry script file path. \CHttpRequest
scriptName string Returns the script name used to access Craft (e.g. “index.php”). HttpRequestService
scriptUrl string Returns the relative URL of the entry script. \CHttpRequest
securePort integer Returns the port to use for secure requests. \CHttpRequest
segments array Returns an array of the Craft path’s segments. HttpRequestService
serverName string Returns the server name. \CHttpRequest
serverPort integer Returns the server port number. \CHttpRequest
token string / null Returns the request’s token, if there is one. HttpRequestService
url string Returns the currently requested URL. \CHttpRequest
urlReferrer string Returns the URL referrer, null if not present \CHttpRequest
userAgent string Returns the user agent, null if not present. \CHttpRequest
userHost string Returns the user host name, null if it cannot be determined. \CHttpRequest
userHostAddress string Alias of getIpAddress(). HttpRequestService
Protected Properties
Property Type Description Defined By
isDeleteViaPostRequest bool Returns whether this is a DELETE request which was tunneled through POST. \CHttpRequest
isPatchViaPostRequest bool Returns whether this is a PATCH request which was tunneled through POST. \CHttpRequest
isPutViaPostRequest bool Returns whether this is a PUT request which was tunneled through POST. \CHttpRequest
Public Methods
Method Description Defined By
__call() Calls the named method which is not a class method. \CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. \CComponent
__isset() Checks if a property value is null. \CComponent
__set() Sets value of a component property. \CComponent
__unset() Sets a component property to be null. \CComponent
asa() Returns the named behavior object. \CComponent
attachBehavior() Attaches a behavior to this component. \CComponent
attachBehaviors() Attaches a list of behaviors to the component. \CComponent
attachEventHandler() Attaches an event handler to an event. \CComponent
canGetProperty() Determines whether a property can be read. \CComponent
canSetProperty() Determines whether a property can be set. \CComponent
close() Attempts to closes the connection with the HTTP client, without ending PHP script execution. HttpRequestService
compareAcceptTypes() Compare function for determining the preference of accepted MIME type array maps \CHttpRequest
decodePathInfo() Decodes the path info. HttpRequestService
deleteCookie() Deletes a cookie by its name. HttpRequestService
detachBehavior() Detaches a behavior from the component. \CComponent
detachBehaviors() Detaches all behaviors from the component. \CComponent
detachEventHandler() Detaches an existing event handler. \CComponent
disableBehavior() Disables an attached behavior. \CComponent
disableBehaviors() Disables all behaviors attached to this component. \CComponent
enableBehavior() Enables an attached behavior. \CComponent
enableBehaviors() Enables all behaviors attached to this component. \CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. \CComponent
getAcceptTypes() Returns user browser accept types, null if not present. \CHttpRequest
getActionSegments() Returns an array of the action path segments, if this is an action request. HttpRequestService
getBaseUrl() Returns the relative URL for the application. \CHttpRequest
getBrowser() Returns information about the capabilities of user browser. \CHttpRequest
getBrowserLanguages() Returns a list of languages the user has selected in their browser’s settings, canonicalized using LocaleData::getCanonicalID. HttpRequestService
getClientOs() Returns whether the client is running "Windows", "Mac", "Linux" or "Other", based on the browser's UserAgent string. HttpRequestService
getContentType() Returns request content-type \CHttpRequest
getCookie() Returns a cookie by its name. HttpRequestService
getCookies() Returns the cookie collection. The result can be used like an associative array. Adding HttpCookie objects to the collection will send the cookies to the client; and removing the objects from the collection will delete those cookies on the client. HttpRequestService
getCsrfToken() Gets the current CSRF token from the CSRF token cookie, (re)creating the cookie if it is missing or invalid. HttpRequestService
getDelete() Returns the named DELETE parameter value. \CHttpRequest
getEventHandlers() Returns the list of attached event handlers for an event. \CComponent
getHostInfo() Returns the schema and host part of the application URL. \CHttpRequest
getHostName() Returns the host name, without “http://” or “https://”. HttpRequestService
getHttpVersion() Returns the version of the HTTP protocol used by client. \CHttpRequest
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. HttpRequestService
getIsAjaxRequest() Returns whether this is an AJAX (XMLHttpRequest) request. \CHttpRequest
getIsDeleteRequest() Returns whether this is a DELETE request. \CHttpRequest
getIsFlashRequest() Returns whether this is an Adobe Flash or Adobe Flex request. \CHttpRequest
getIsGetRequest() Returns whether this is a GET request. HttpRequestService
getIsInitialized() Checks if this application component has been initialized. \CApplicationComponent
getIsPatchRequest() Returns whether this is a PATCH request. \CHttpRequest
getIsPostRequest() Returns whether this is a POST request. \CHttpRequest
getIsPutRequest() Returns whether this is a PUT request. \CHttpRequest
getIsSecureConnection() Return if the request is sent via secure channel (https). \CHttpRequest
getMimeType() Returns the MIME type that is going to be included in the response via the Content-Type header. HttpRequestService
getNormalizedPath() Returns the path Craft should use to route this request, including the CP trigger if it is in there. HttpRequestService
getPageNum() Returns the current page number. HttpRequestService
getParam() Returns a parameter from either the query string or POST data. HttpRequestService
getPatch() Returns the named PATCH parameter value. \CHttpRequest
getPath() Returns the request’s Craft path. HttpRequestService
getPathInfo() Returns the path info of the currently requested URL. \CHttpRequest
getPort() Returns the port to use for insecure requests. \CHttpRequest
getPost() Returns a POST parameter, or all of them. HttpRequestService
getPreferredAcceptType() Returns the user preferred accept MIME type. \CHttpRequest
getPreferredAcceptTypes() Returns an array of user accepted MIME types in order of preference. \CHttpRequest
getPreferredLanguage() Returns the user-preferred language that should be used by this application. \CHttpRequest
getPreferredLanguages() Returns an array of user accepted languages in order of preference. \CHttpRequest
getPut() Returns the named PUT parameter value. \CHttpRequest
getQuery() Returns a query string parameter, or all of them. HttpRequestService
getQueryString() Returns part of the request URL that is after the question mark. \CHttpRequest
getQueryStringWithoutPath() Returns the request’s query string, without the p= parameter. HttpRequestService
getRawBody() Returns the raw HTTP request body. \CHttpRequest
getRequestType() Returns the request type, such as GET, POST, HEAD, PUT, PATCH, DELETE. \CHttpRequest
getRequestUri() Returns the request URI portion for the currently requested URL. \CHttpRequest
getRequiredParam() Returns a parameter from either the query string or POST data, or bails on the request with a 400 error if that parameter doesn’t exist anywhere. HttpRequestService
getRequiredPost() Returns a POST parameter, or bails on the request with a 400 error if that parameter doesn’t exist. HttpRequestService
getRequiredQuery() Returns a query string parameter, or bails on the request with a 400 error if that parameter doesn’t exist. HttpRequestService
getRestParams() Returns request parameters. Typically PUT, PATCH or DELETE. \CHttpRequest
getScriptFile() Returns entry script file path. \CHttpRequest
getScriptName() Returns the script name used to access Craft (e.g. “index.php”). HttpRequestService
getScriptUrl() Returns the relative URL of the entry script. \CHttpRequest
getSecurePort() Returns the port to use for secure requests. \CHttpRequest
getSegment() Returns a specific segment from the Craft path. HttpRequestService
getSegments() Returns an array of the Craft path’s segments. HttpRequestService
getServerName() Returns the server name. \CHttpRequest
getServerPort() Returns the server port number. \CHttpRequest
getToken() Returns the request’s token, if there is one. HttpRequestService
getUrl() Returns the currently requested URL. \CHttpRequest
getUrlReferrer() Returns the URL referrer, null if not present \CHttpRequest
getUserAgent() Returns the user agent, null if not present. \CHttpRequest
getUserHost() Returns the user host name, null if it cannot be determined. \CHttpRequest
getUserHostAddress() Alias of getIpAddress(). HttpRequestService
getValidatedPost() Returns a POST parameter. If the validateUnsafeRequestParams config setting has been set to true, and this is a front-end request, then the POST parameter’s value will be validated with SecurityService::validateData() before being returned, ensuring that the value had not been tampered with by the user. HttpRequestService
hasEvent() Determines whether an event is defined. \CComponent
hasEventHandler() Checks whether the named event has attached handlers. \CComponent
hasProperty() Determines whether a property is defined. \CComponent
init() Initializes the application component. HttpRequestService
isActionRequest() Returns whether the current request should be routed to a specific controller action before normal request routing takes over. HttpRequestService
isAjaxRequest() Alias of getIsAjaxRequest(). HttpRequestService
isCpRequest() Returns whether the current request should be routed to the Control Panel. HttpRequestService
isDeleteRequest() Alias of getIsDeleteRequest(). HttpRequestService
isDeleteViaPostRequest() Alias of getIsDeleteViaPostRequest(). HttpRequestService
isFlashRequest() Alias of getIsFlashRequest(). HttpRequestService
isGetRequest() Alias of getIsGetRequest(). HttpRequestService
isLivePreview() Returns whether this is a Live Preview request. HttpRequestService
isMobileBrowser() Returns whether the request is coming from a mobile browser. HttpRequestService
isPostRequest() Alias of getIsPostRequest(). HttpRequestService
isPutRequest() Alias of getIsPutRequest(). HttpRequestService
isPutViaPostRequest() Alias of getIsPutViaPostRequest(). HttpRequestService
isResourceRequest() Returns whether the current request should be routed to a resource. HttpRequestService
isSecureConnection() Alias of getIsSecureConnection(). HttpRequestService
isSingleActionRequest() Returns whether the current request is solely an action request. HttpRequestService
isSiteRequest() Returns whether the current request should be routed to the front-end site. HttpRequestService
parseAcceptHeader() Parses an HTTP Accept header, returning an array map with all parts of each entry. \CHttpRequest
raiseEvent() Raises an event. \CComponent
redirect() Redirects the browser to the specified URL. \CHttpRequest
regenCsrfCookie() HttpRequestService
sendFile() Sends a file to the user. HttpRequestService
setBaseUrl() Sets the relative URL for the application. \CHttpRequest
setHostInfo() Sets the schema and host part of the application URL. \CHttpRequest
setPort() Sets the port to use for insecure requests. \CHttpRequest
setScriptUrl() Sets the relative URL for the application entry script. \CHttpRequest
setSecurePort() Sets the port to use for secure requests. \CHttpRequest
stripSlashes() Strips slashes from input data. \CHttpRequest
validateCsrfToken() Performs the CSRF validation. This is the event handler responding to CApplication::onBeginRequest. The default implementation will compare the CSRF token obtained from session and from a POST field. If they are different, a CSRF attack is detected. HttpRequestService
xSendFile() Sends existing file to a browser as a download using x-sendfile. \CHttpRequest
Protected Methods
Method Description Defined By
createCsrfCookie() Creates a cookie with a randomly generated CSRF token. Initial values specified in csrfCookie will be applied to the generated cookie. HttpRequestService
csrfTokenValidForCurrentUser() Gets whether the CSRF token is valid for the current user or not HttpRequestService
getIsDeleteViaPostRequest() Returns whether this is a DELETE request which was tunneled through POST. \CHttpRequest
getIsPatchViaPostRequest() Returns whether this is a PATCH request which was tunneled through POST. \CHttpRequest
getIsPutViaPostRequest() Returns whether this is a PUT request which was tunneled through POST. \CHttpRequest
normalizeRequest() Normalizes the request data. \CHttpRequest

Property Details

actionSegments public property #

Returns an array of the action path segments, if this is an action request.

public array|null getActionSegments()

browserLanguages public property #

Returns a list of languages the user has selected in their browser’s settings, canonicalized using LocaleData::getCanonicalID.

Internally, this method checks the Accept-Language header that should have accompanied the request. If that header was not present, the method will return false.

public array|false getBrowserLanguages()

clientOs public property #

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

public string getClientOs()

cookies public property #

Returns the cookie collection. The result can be used like an associative array. Adding HttpCookie objects to the collection will send the cookies to the client; and removing the objects from the collection will delete those cookies on the client.

public CookieCollection getCookies()

csrfToken public property #

Gets the current CSRF token from the CSRF token cookie, (re)creating the cookie if it is missing or invalid.

public string getCsrfToken()

hostName public property #

Returns the host name, without “http://” or “https://”.

Internally, this method will first check the Host header that should have accompanied the request, which browsers will set depending on the host name they are requesting. If that header does not exist, the method will fall back on the SERVER_NAME server environment variable.

public string getHostName()

ipAddress public property #

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.

public string getIpAddress()

isGetRequest public property #

Returns whether this is a GET request.

public bool getIsGetRequest()

mimeType public property #

Returns the MIME type that is going to be included in the response via the Content-Type header.

Deprecated in 2.2. Use HeaderHelper::getMimeType() instead.

public string getMimeType()

normalizedPath public property #

Returns the path Craft should use to route this request, including the CP trigger if it is in there.

public string getNormalizedPath()

pageNum public property #

Returns the current page number.

public int getPageNum()

path public property #

Returns the request’s Craft path.

Note that the path will not include the CP trigger if it’s a CP request, or the page trigger or page number if it’s a paginated request.

public string getPath()

post public property #

Returns a POST parameter, or all of them.

If $name is specified, then the corresponding POST parameter will be returned if it exists, or $defaultValue will be returned if it doesn’t.

$foo = craft()->request->getPost('foo'); // Returns $_POST['foo'], if it exists

$name can also represent a nested parameter using a dot-delimited string.

$bar = craft()->request->getPost('foo.bar'); // Returns $_POST['foo']['bar'], if it exists

If $name is omitted, the entire $_POST array will be returned instead:

$allThePostParams = craft()->request->getPost(); // Returns $_POST

All values will be converted to UTF-8, regardless of the original character encoding.

public mixed getPost(string|null $name = null, mixed $defaultValue = null)

query public property #

Returns a query string parameter, or all of them.

If $name is specified, then the corresponding query string parameter will be returned if it exists, or $defaultValue will be returned if it doesn’t.

$foo = craft()->request->getQuery('foo'); // Returns $_GET['foo'], if it exists

$name can also represent a nested parameter using a dot-delimited string.

$bar = craft()->request->getQuery('foo.bar'); // Returns $_GET['foo']['bar'], if it exists

If $name is omitted, the entire $_GET array will be returned instead:

$allTheQueryParams = craft()->request->getQuery(); // Returns $_GET

All values will be converted to UTF-8, regardless of the original character encoding.

public mixed getQuery(string|null $name = null, mixed $defaultValue = null)

queryStringWithoutPath public property #

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

public string getQueryStringWithoutPath()

scriptName public property #

Returns the script name used to access Craft (e.g. “index.php”).

public string getScriptName()

segments public property #

Returns an array of the Craft path’s segments.

Note that the segments will not include the CP trigger if it’s a CP request, or the page trigger or page number if it’s a paginated request.

public array getSegments()

token public property #

Returns the request’s token, if there is one.

public string|null getToken()

userHostAddress public property #

Alias of getIpAddress().

public string getUserHostAddress()

Method Details

close() public method #

Attempts to closes the connection with the HTTP client, without ending PHP script execution.

This method relies on flush(), which may not actually work if mod_deflate or mod_gzip is installed, or if this is a Win32 server.

public null close(string|null $content = '')
$content string / null Any content that should be included in the response body.

createCsrfCookie() protected method #

Creates a cookie with a randomly generated CSRF token. Initial values specified in csrfCookie will be applied to the generated cookie.

protected HttpCookie createCsrfCookie()
Returns HttpCookie The generated cookie

csrfTokenValidForCurrentUser() protected method #

Gets whether the CSRF token is valid for the current user or not

protected bool csrfTokenValidForCurrentUser($token $token)
$token $token
Returns bool

decodePathInfo() public method #

Decodes the path info.

Replacement for Yii's \CHttpRequest::decodePathInfo().

public string decodePathInfo(string $pathInfo)
$pathInfo string Encoded path info.
Returns string Decoded path info.

deleteCookie() public method #

Deletes a cookie by its name.

public null deleteCookie($name $name)
$name $name The cookie name.

getActionSegments() public method #

Returns an array of the action path segments, if this is an action request.

public array|null getActionSegments()
Returns array / null The action path segments, or null if this isn’t an action request.

getBrowserLanguages() public method #

Returns a list of languages the user has selected in their browser’s settings, canonicalized using LocaleData::getCanonicalID.

Internally, this method checks the Accept-Language header that should have accompanied the request. If that header was not present, the method will return false.

public array|false getBrowserLanguages()
Returns array / false The preferred languages, or false if Craft is unable to determine them.

getClientOs() public method #

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

public string getClientOs()
Returns string The OS the client is running.

getCookie() public method #

Returns a cookie by its name.

public HttpCookie|null getCookie(string $name)
$name string The cookie name.
Returns HttpCookie / null The cookie, or null if it didn’t exist.

getCookies() public method #

Returns the cookie collection. The result can be used like an associative array. Adding HttpCookie objects to the collection will send the cookies to the client; and removing the objects from the collection will delete those cookies on the client.

public CookieCollection getCookies()
Returns CookieCollection The cookie collection.

getCsrfToken() public method #

Gets the current CSRF token from the CSRF token cookie, (re)creating the cookie if it is missing or invalid.

public string getCsrfToken()
Returns string

getHostName() public method #

Returns the host name, without “http://” or “https://”.

Internally, this method will first check the Host header that should have accompanied the request, which browsers will set depending on the host name they are requesting. If that header does not exist, the method will fall back on the SERVER_NAME server environment variable.

public string getHostName()
Returns string The host name.

getIpAddress() public method #

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.

public string getIpAddress()
Returns string The IP address.

getIsGetRequest() public method #

Returns whether this is a GET request.

public bool getIsGetRequest()
Returns bool Whether this is a GET request.

getMimeType() public method #

Returns the MIME type that is going to be included in the response via the Content-Type header.

Deprecated in 2.2. Use HeaderHelper::getMimeType() instead.

public string getMimeType()
Returns string

getNormalizedPath() public method #

Returns the path Craft should use to route this request, including the CP trigger if it is in there.

public string getNormalizedPath()
Returns string The path.

getPageNum() public method #

Returns the current page number.

public int getPageNum()
Returns int The page number.

getParam() public method #

Returns a parameter from either the query string or POST data.

This method will first search for the given paramater in the query string, calling getQuery() internally, and if that doesn’t come back with a value, it will call getPost(). If that doesn’t come back with a value either, $defaultValue will be returned.

$foo = craft()->request->getParam('foo'); // Returns $_GET['foo'] or $_POST['foo'], if either exist

$name can also represent a nested parameter using a dot-delimited string.

$bar = craft()->request->getParam('foo.bar'); // Returns $_GET['foo']['bar'] or $_POST['foo']['bar'], if either exist

All values will be converted to UTF-8, regardless of the original character encoding.

public mixed getParam(string $name, mixed $defaultValue = null)
$name string The dot-delimited name of the param to be fetched.
$defaultValue mixed The fallback value to be returned if no param exists by the given $name. Defaults to null.
Returns mixed The value of the corresponding param, or $defaultValue if that value didn’t exist.

getPath() public method #

Returns the request’s Craft path.

Note that the path will not include the CP trigger if it’s a CP request, or the page trigger or page number if it’s a paginated request.

public string getPath()
Returns string The Craft path.

getPost() public method #

Returns a POST parameter, or all of them.

If $name is specified, then the corresponding POST parameter will be returned if it exists, or $defaultValue will be returned if it doesn’t.

$foo = craft()->request->getPost('foo'); // Returns $_POST['foo'], if it exists

$name can also represent a nested parameter using a dot-delimited string.

$bar = craft()->request->getPost('foo.bar'); // Returns $_POST['foo']['bar'], if it exists

If $name is omitted, the entire $_POST array will be returned instead:

$allThePostParams = craft()->request->getPost(); // Returns $_POST

All values will be converted to UTF-8, regardless of the original character encoding.

public mixed getPost(string|null $name = null, mixed $defaultValue = null)
$name string / null The dot-delimited name of the POST param to be fetched, if any.
$defaultValue mixed The fallback value to be returned if no param exists by the given $name. Defaults to null.
Returns mixed The value of the corresponding POST param if a single param was requested, or $defaultValue if that value didn’t exist, or the entire $_POST array if no single param was requested.

getQuery() public method #

Returns a query string parameter, or all of them.

If $name is specified, then the corresponding query string parameter will be returned if it exists, or $defaultValue will be returned if it doesn’t.

$foo = craft()->request->getQuery('foo'); // Returns $_GET['foo'], if it exists

$name can also represent a nested parameter using a dot-delimited string.

$bar = craft()->request->getQuery('foo.bar'); // Returns $_GET['foo']['bar'], if it exists

If $name is omitted, the entire $_GET array will be returned instead:

$allTheQueryParams = craft()->request->getQuery(); // Returns $_GET

All values will be converted to UTF-8, regardless of the original character encoding.

public mixed getQuery(string|null $name = null, mixed $defaultValue = null)
$name string / null The dot-delimited name of the query string param to be fetched, if any.
$defaultValue mixed The fallback value to be returned if no param exists by the given $name. Defaults to null.
Returns mixed The value of the corresponding query string param if a single param was requested, or $defaultValue if that value didn’t exist, or the entire $_GET array if no single param was requested.

getQueryStringWithoutPath() public method #

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

public string getQueryStringWithoutPath()
Returns string The query string.

getRequiredParam() public method #

Returns a parameter from either the query string or POST data, or bails on the request with a 400 error if that parameter doesn’t exist anywhere.

This method will first search for the given paramater in the query string, calling getQuery() internally, and if that doesn’t come back with a value, it will call getPost().

$foo = craft()->request->getRequiredParam('foo'); // Returns $_GET['foo'] or $_POST['foo']

$name can also represent a nested parameter using a dot-delimited string.

$bar = craft()->request->getParam('foo.bar'); // Returns $_GET['foo']['bar'] or $_POST['foo']['bar'], if either exist

All values will be converted to UTF-8, regardless of the original character encoding.

public mixed getRequiredParam(string $name)
$name string The dot-delimited name of the param to be fetched.
Returns mixed The value of the corresponding param, or $defaultValue if that value didn’t exist.

getRequiredPost() public method #

Returns a POST parameter, or bails on the request with a 400 error if that parameter doesn’t exist.

$foo = craft()->request->getRequiredPost('foo'); // Returns $_POST['foo']

$name can also represent a nested parameter using a dot-delimited string.

$bar = craft()->request->getRequiredPost('foo.bar'); // Returns $_POST['foo']['bar']

The returned value will be converted to UTF-8, regardless of the original character encoding.

public mixed getRequiredPost(string $name)
$name string The dot-delimited name of the POST param to be fetched.
Returns mixed The value of the corresponding POST param.

getRequiredQuery() public method #

Returns a query string parameter, or bails on the request with a 400 error if that parameter doesn’t exist.

$foo = craft()->request->getRequiredQuery('foo'); // Returns $_GET['foo']

$name can also represent a nested parameter using a dot-delimited string.

$bar = craft()->request->getRequiredQuery('foo.bar'); // Returns $_GET['foo']['bar']

The returned value will be converted to UTF-8, regardless of the original character encoding.

public mixed getRequiredQuery(string $name)
$name string The dot-delimited name of the query string param to be fetched.
Returns mixed The value of the corresponding query string param.

getScriptName() public method #

Returns the script name used to access Craft (e.g. “index.php”).

public string getScriptName()
Returns string

getSegment() public method #

Returns a specific segment from the Craft path.

public string|null getSegment(int $num)
$num int Which segment to return (1-indexed).
Returns string / null The matching segment, or null if there wasn’t one.

getSegments() public method #

Returns an array of the Craft path’s segments.

Note that the segments will not include the CP trigger if it’s a CP request, or the page trigger or page number if it’s a paginated request.

public array getSegments()
Returns array The Craft path’s segments.

getToken() public method #

Returns the request’s token, if there is one.

public string|null getToken()
Returns string / null The request’s token, or null if there isn’t one.

getUserHostAddress() public method #

Alias of getIpAddress().

public string getUserHostAddress()
Returns string

getValidatedPost() public method #

Returns a POST parameter. If the validateUnsafeRequestParams config setting has been set to true, and this is a front-end request, then the POST parameter’s value will be validated with SecurityService::validateData() before being returned, ensuring that the value had not been tampered with by the user.

public mixed getValidatedPost(string $name)
$name string The dot-delimited name of the POST param to be fetched.
Returns mixed The value of the corresponding POST param

init() public method #

Initializes the application component.

public null init()

isActionRequest() public method #

Returns whether the current request should be routed to a specific controller action before normal request routing takes over.

There are several ways that this method could return true:

public bool isActionRequest()
Returns bool Whether the current request should be routed to a controller action.

isAjaxRequest() public method #

Alias of getIsAjaxRequest().

public bool isAjaxRequest()
Returns bool

isCpRequest() public method #

Returns whether the current request should be routed to the Control Panel.

The result depends on whether the first segment in the URI matches the CP trigger.

Note that even if this function returns true, the request will not necessarily route to the Control Panel. It could instead route to a resource, for example.

public bool isCpRequest()
Returns bool Whether the current request should be routed to the Control Panel.

isDeleteRequest() public method #

Alias of getIsDeleteRequest().

public bool isDeleteRequest()
Returns bool

isDeleteViaPostRequest() public method #

Alias of getIsDeleteViaPostRequest().

public bool isDeleteViaPostRequest()
Returns bool

isFlashRequest() public method #

Alias of getIsFlashRequest().

public bool isFlashRequest()
Returns bool

isGetRequest() public method #

Alias of getIsGetRequest().

public void isGetRequest()

isLivePreview() public method #

Returns whether this is a Live Preview request.

public bool isLivePreview()
Returns bool Whether this is a Live Preview request.

isMobileBrowser() public method #

Returns whether the request is coming from a mobile browser.

The detection script is provided by http://detectmobilebrowsers.com. It was last updated on 2014-11-24.

public bool isMobileBrowser(bool $detectTablets = false)
$detectTablets bool Whether tablets should be considered “modile”.
Returns bool Whether the request is coming from a mobile browser.

isPostRequest() public method #

Alias of getIsPostRequest().

public bool isPostRequest()
Returns bool

isPutRequest() public method #

Alias of getIsPutRequest().

public bool isPutRequest()
Returns bool

isPutViaPostRequest() public method #

Alias of getIsPutViaPostRequest().

public bool isPutViaPostRequest()
Returns bool

isResourceRequest() public method #

Returns whether the current request should be routed to a resource.

The result depends on whether the first segment in the Craft path matches the resource trigger.

public bool isResourceRequest()
Returns bool Whether the current request should be routed to a resource.

isSecureConnection() public method #

Alias of getIsSecureConnection().

public bool isSecureConnection()
Returns bool

isSingleActionRequest() public method #

Returns whether the current request is solely an action request.

public void isSingleActionRequest()

isSiteRequest() public method #

Returns whether the current request should be routed to the front-end site.

The result will always just be the opposite of whatever isCpRequest() returns.

public bool isSiteRequest()
Returns bool Whether the current request should be routed to the front-end site.

regenCsrfCookie() public method #

public void regenCsrfCookie()

sendFile() public method #

Sends a file to the user.

We’re overriding this from \CHttpRequest::sendFile() so we can have more control over the headers.

public null sendFile(string $path, string $content, array|null $options = array (
), bool|null $terminate = true)
$path string The path to the file on the server.
$content string The contents of the file.
$options array / null An array of optional options. Possible keys include 'forceDownload', 'mimeType', and 'cache'.
$terminate bool / null Whether the request should be terminated after the file has been sent. Defaults to true.

validateCsrfToken() public method #

Performs the CSRF validation. This is the event handler responding to CApplication::onBeginRequest. The default implementation will compare the CSRF token obtained from session and from a POST field. If they are different, a CSRF attack is detected.

public void validateCsrfToken(Event $event)
$event Event event parameter