Request
API reference › Inertia › web › Request
Configures CSRF protection for Inertia applications using the cookie-to-header pattern.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$absoluteUrl read-only | string | The currently requested absolute URL. | yii\ |
$acceptableContentTypes | array | The content types ordered by the quality score. | yii\ |
$acceptableLanguages | array | The languages ordered by the preference level. | yii\ |
$authCredentials read-only | array | That contains exactly two elements: - 0: the username sent via HTTP authentication, null if the username is not given - 1: the password sent via HTTP authentication, null if the password is not given. | yii\ |
$authPassword read-only | string| | The password sent via HTTP authentication, null if the password is not given. | yii\ |
$authUser read-only | string| | The username sent via HTTP authentication, null if the username is not given. | yii\ |
$baseUrl | string | The relative URL for the application. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$bodyParams | mixed | The request parameters given in the request body. | yii\ |
$contentType read-only | string | Request content-type. | yii\ |
$cookies read-only | yii\ | The cookie collection. | yii\ |
$cookieValidationKey | string | A secret key used for cookie validation. Default: null. | yii\ |
$csrfCookie | mixed[] | Cookie options. httpOnly is false so JavaScript can read the CSRF token. Default: ['httpOnly' => false]. | Request |
$csrfHeader | Header checked for the CSRF token sent by Inertia’s HTTP client. Default: 'X-XSRF-TOKEN'. | Request | |
$csrfHeaderUnsafeMethods | array | “unsafe” methods not triggered a CORS-preflight request This property is used only when both $enableCsrfValidation and $validateCsrfHeaderOnly are true. Default: ['GET',. | yii\ |
$csrfParam | Cookie name and form parameter name for the CSRF token. Default: 'XSRF-TOKEN'. | Request | |
$csrfToken read-only | null| | The token used to perform CSRF validation. | yii\ |
$csrfTokenFromHeader read-only | string| | The CSRF token sent via $csrfHeader by browser. | yii\ |
$csrfTokenSafeMethods | array | The name of the HTTP header for sending CSRF token. by default validate CSRF token on non-”safe” methods only This property is used only when $enableCsrfValidation is true. Default: ['GET',. | yii\ |
$enableCookieValidation | bool | Whether cookies should be validated to ensure they are not tampered. Default: true. | yii\ |
$enableCsrfCookie | bool | Whether to use cookie to persist CSRF token. Default: true. | yii\ |
$enableCsrfValidation | bool | Whether to enable CSRF (Cross-Site Request Forgery) validation. Default: true. | yii\ |
$eTags read-only | array | The entity tags. | yii\ |
$headers read-only | yii\ | The header collection. | yii\ |
$hostInfo | string| | Schema and hostname part (with port number if needed) of the request URL (e.g. https://www.yiiframework.com), null if can’t be obtained from $_SERVER and wasn’t set. | yii\ |
$hostName read-only | string| | Hostname part of the request URL (e.g. www.yiiframework.com). | yii\ |
$ipHeaders | string[] | List of headers where proxies store the real client IP. Default: ['X-Forwarded-For']. Available since 2.0.13. | yii\ |
$isAjax read-only | bool | Whether this is an AJAX (XMLHttpRequest) request. | yii\ |
$isConsoleRequest | bool | The value indicating whether the current request is made via console. | yii\ |
$isDelete read-only | bool | Whether this is a DELETE request. | yii\ |
$isFlash read-only | bool | Whether this is an Adobe Flash or Adobe Flex request. | yii\ |
$isGet read-only | bool | Whether this is a GET request. | yii\ |
$isHead read-only | bool | Whether this is a HEAD request. | yii\ |
$isOptions read-only | bool | Whether this is a OPTIONS request. | yii\ |
$isPatch read-only | bool | Whether this is a PATCH request. | yii\ |
$isPjax read-only | bool | Whether this is a PJAX request. | yii\ |
$isPost read-only | bool | Whether this is a POST request. | yii\ |
$isPut read-only | bool | Whether this is a PUT request. | yii\ |
$isSecureConnection read-only | bool | If the request is sent via secure channel (https). | yii\ |
$method read-only | string | Request method, such as GET, POST, HEAD, PUT, PATCH, DELETE. | yii\ |
$methodParam | string | The name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE request tunneled through POST. Default: '_method'. | yii\ |
$origin read-only | string| | URL origin of a CORS request, null if not available. | yii\ |
$parsers | array | The parsers for converting the raw HTTP request body into $bodyParams. Default: []. | yii\ |
$pathInfo | string | Part of the request URL that is after the entry script and before the question mark. | yii\ |
$port | int | Port number for insecure requests. | yii\ |
$portHeaders | string[] | List of headers where proxies store the real request port. Default: ['X-Forwarded-Port']. Available since 2.0.46. | yii\ |
$preferredLanguage read-only | string | The language that the application should use. | yii\ |
$queryParams | array | The request GET parameter values. | yii\ |
$queryString read-only | string | Part of the request URL that is after the question mark. | yii\ |
$rawBody | string | The request body. | yii\ |
$referrer read-only | string| | URL referrer, null if not available. | yii\ |
$remoteHost read-only | string| | Remote host name, null if not available. | yii\ |
$remoteIP read-only | string| | Remote IP address, null if not available. | yii\ |
$scriptFile | string | The entry script file path. | yii\ |
$scriptUrl | string | The relative URL of the entry script. | yii\ |
$secureHeaders | array | Lists of headers that are, by default, subject to the trusted host configuration. Default: ['X-Forwarded-For',. Available since 2.0.13. | yii\ |
$securePort | int | Port number for secure requests. | yii\ |
$secureProtocolHeaders | array | List of headers to check for determining whether the connection is made via HTTPS. Default: ['X-Forwarded-Proto' => ['https'],. Available since 2.0.13. | yii\ |
$serverName read-only | string| | Server name, null if not available. | yii\ |
$serverPort read-only | int| | Server port number, null if not available. | yii\ |
$trustedHosts | array | The configuration for trusted security related headers. Default: []. Available since 2.0.13. | yii\ |
$url | string | The currently requested relative URL. | yii\ |
$userAgent read-only | string| | User agent, null if not available. | yii\ |
$userHost read-only | string| | User host name, null if not available. | yii\ |
$userIP read-only | string| | User IP address, null if not available. | yii\ |
$validateCsrfHeaderOnly | bool | Whether to use custom header only to CSRF validation of SPA. Default: false. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
get() | Returns GET parameter with a given name. If name isn’t specified, returns an array of all GET parameters. | yii\ |
getAbsoluteUrl() | Returns the currently requested absolute URL. | yii\ |
getAcceptableContentTypes() | Returns the content types acceptable by the end user. | yii\ |
getAcceptableLanguages() | Returns the languages acceptable by the end user. | yii\ |
getAuthCredentials() | yii\ | |
getAuthPassword() | yii\ | |
getAuthUser() | yii\ | |
getBaseUrl() | Returns the relative URL for the application. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getBodyParam() | Returns the named request body parameter value. | yii\ |
getBodyParams() | Returns the request parameters given in the request body. | yii\ |
getContentType() | Returns request content-type The Content-Type header field indicates the MIME type of the data contained in getRawBody() or, in the case of the HEAD method, the media type that would have been sent had the request been a GET. | yii\ |
getCookies() | Returns the cookie collection. | yii\ |
getCsrfToken() | Returns the token used to perform CSRF validation. | yii\ |
getCsrfTokenFromHeader() | Returns the CSRF token sent via the X-XSRF-TOKEN header. | Request |
getETags() | Gets the Etags. | yii\ |
getHeaders() | Returns the header collection. | yii\ |
getHostInfo() | Returns the schema and host part of the current request URL. | yii\ |
getHostName() | Returns the host part of the current request URL. | yii\ |
getIsAjax() | Returns whether this is an AJAX (XMLHttpRequest) request. | yii\ |
getIsConsoleRequest() | Returns a value indicating whether the current request is made via command line. | yii\ |
getIsDelete() | Returns whether this is a DELETE request. | yii\ |
getIsFlash() | Returns whether this is an Adobe Flash or Flex request. | yii\ |
getIsGet() | Returns whether this is a GET request. | yii\ |
getIsHead() | Returns whether this is a HEAD request. | yii\ |
getIsOptions() | Returns whether this is an OPTIONS request. | yii\ |
getIsPatch() | Returns whether this is a PATCH request. | yii\ |
getIsPjax() | Returns whether this is a PJAX request. | yii\ |
getIsPost() | Returns whether this is a POST request. | yii\ |
getIsPut() | Returns whether this is a PUT request. | yii\ |
getIsSecureConnection() | Return if the request is sent via secure channel (https). | yii\ |
getMethod() | Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE). | yii\ |
getOrigin() | Returns the URL origin of a CORS request. | yii\ |
getPathInfo() | Returns the path info of the currently requested URL. | yii\ |
getPort() | Returns the port to use for insecure requests. | yii\ |
getPreferredLanguage() | Returns the user-preferred language that should be used by this application. | yii\ |
getQueryParam() | Returns the named GET parameter value. | yii\ |
getQueryParams() | Returns the request parameters given in the $queryString. | yii\ |
getQueryString() | Returns part of the request URL that is after the question mark. | yii\ |
getRawBody() | Returns the raw HTTP request body. | yii\ |
getReferrer() | Returns the URL referrer. | yii\ |
getRemoteHost() | Returns the host name of the other end of this connection. | yii\ |
getRemoteIP() | Returns the IP on the other end of this connection. | yii\ |
getScriptFile() | Returns the entry script file path. | yii\ |
getScriptUrl() | Returns the relative URL of the entry script. | yii\ |
getSecurePort() | Returns the port to use for secure requests. | yii\ |
getServerName() | Returns the server name. | yii\ |
getServerPort() | Returns the server port number. If a port is specified via a forwarding header (e.g. ‘X-Forwarded-Port’) and the remote host is a “trusted host” the that port will be used (see $portHeaders), otherwise the default server port will be returned. | yii\ |
getUrl() | Returns the currently requested relative URL. | yii\ |
getUserAgent() | Returns the user agent. | yii\ |
getUserHost() | Returns the user host name. | yii\ |
getUserIP() | Returns the user IP address. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
parseAcceptHeader() | Parses the given Accept (or Accept-Language) header. | yii\ |
post() | Returns POST parameter with a given name. If name isn’t specified, returns an array of all POST parameters. | yii\ |
resolve() | Resolves the current request into a route and the associated parameters. | yii\ |
setAcceptableContentTypes() | Sets the acceptable content types. | yii\ |
setAcceptableLanguages() | yii\ | |
setBaseUrl() | Sets the relative URL for the application. | yii\ |
setBodyParams() | Sets the request body parameters. | yii\ |
setHostInfo() | Sets the schema and host part of the application URL. | yii\ |
setIsConsoleRequest() | Sets the value indicating whether the current request is made via command line. | yii\ |
setPathInfo() | Sets the path info of the current request. | yii\ |
setPort() | Sets the port to use for insecure requests. | yii\ |
setQueryParams() | Sets the request $queryString parameters. | yii\ |
setRawBody() | Sets the raw HTTP request body, this method is mainly used by test scripts to simulate raw HTTP requests. | yii\ |
setScriptFile() | Sets the entry script file path. | yii\ |
setScriptUrl() | Sets the relative URL for the application entry script. | yii\ |
setSecurePort() | Sets the port to use for secure requests. | yii\ |
setUrl() | Sets the currently requested relative URL. | yii\ |
trigger() | Triggers an event. | yii\ |
validateCsrfToken() | Performs the CSRF validation. | yii\ |
createCsrfCookie() protected | Creates a cookie with a randomly generated CSRF token. | yii\ |
filterHeaders() protected | Filters headers according to the $trustedHosts. | yii\ |
generateCsrfToken() protected | Generates an unmasked random token used to perform CSRF validation. | yii\ |
getIpValidator() protected | Creates instance of IpValidator. | yii\ |
getSecureForwardedHeaderParts() protected | Returns decoded forwarded header | yii\ |
getSecureForwardedHeaderTrustedPart() protected | Gets first Forwarded header value for token | yii\ |
getSecureForwardedHeaderTrustedParts() protected | Gets only trusted Forwarded header parts | yii\ |
getTrustedHeaders() protected | Trusted headers according to the $trustedHosts. | yii\ |
getUserIpFromIpHeader() protected | Return user IP’s from IP header. | yii\ |
getUserIpFromIpHeaders() protected | Returns the user IP address from $ipHeaders. | yii\ |
loadCookies() protected | Converts $_COOKIE into an array of Cookie. | yii\ |
loadCsrfToken() protected | Loads the CSRF token from cookie or session. | yii\ |
resolvePathInfo() protected | Resolves the path info part of the currently requested URL. | yii\ |
resolveRequestUri() protected | Resolves the request URI portion for the currently requested URL. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
CSRF_HEADER | 'X-CSRF-Token' | Default name of the HTTP header for sending CSRF token. | yii\ |
Method details
Section titled “Method details”getCsrfTokenFromHeader()
Section titled “getCsrfTokenFromHeader() ”public function getCsrfTokenFromHeader(): string|nullReturns the CSRF token sent via the X-XSRF-TOKEN header.
Returns
string|null: Masked token after signed-cookie validation, raw header when cookie validation is disabled, or null when the header is absent or fails signed-cookie validation.