Cors
API reference › yii › filters › Cors
Cors filter implements Cross Origin Resource Sharing.
Make sure to read carefully what CORS does and does not. CORS do not secure your API, but allow the developer to grant access to third party code (ajax calls from external domain).
You may use CORS filter by attaching it as a behavior to a controller or module, like the following,
public function behaviors(){ return [ 'corsFilter' => [ 'class' => \yii\filters\Cors::class, ], ];}The CORS filter can be specialized to restrict parameters, like this, MDN CORS Information
public function behaviors(){ return [ 'corsFilter' => [ 'class' => \yii\filters\Cors::class, 'cors' => [ // restrict access to 'Origin' => ['http://www.myserver.com', 'https://www.myserver.com'], // Allow only POST and PUT methods 'Access-Control-Request-Method' => ['POST', 'PUT'], // Allow only headers 'X-Wsse' 'Access-Control-Request-Headers' => ['X-Wsse'], // Allow credentials (cookies, authorization headers, etc.) to be exposed to the browser 'Access-Control-Allow-Credentials' => true, // Allow OPTIONS caching 'Access-Control-Max-Age' => 3600, // Allow the X-Pagination-Current-Page header to be exposed to the browser. 'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page'], ],
], ];}For more information on how to add the CORS filter to a controller, see the Guide on REST controllers.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$actions | array | Define specific CORS rules for specific actions Default: []. | Cors |
$cors | array | Basic headers handled for the CORS requests. Default: ['Origin' => ['*'],. | Cors |
$except | array | List of action IDs that this filter should not apply to. Default: []. | yii\ |
$only | array | List of action IDs that this filter should apply to. Default: []. | yii\ |
$owner | yii\ | The owner of this behavior Default: null. | yii\ |
$request | yii\| | The current request. Default: null. | Cors |
$response | yii\| | The response to be sent. Default: null. | Cors |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
addCorsHeaders() | Adds the CORS headers to the response. | Cors |
afterAction() | This method is invoked right after an action is executed. | yii\ |
afterFilter() | yii\ | |
attach() | Attaches the behavior object to the component. | yii\ |
beforeAction() | This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action. | Cors |
beforeFilter() | 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\ |
detach() | Detaches the behavior object from the component. | yii\ |
events() | Declares event handlers for the $owner’s events. | yii\ |
extractHeaders() | Extract CORS headers from the request. | Cors |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
overrideDefaultSettings() | Override settings for specific action. | Cors |
prepareHeaders() | For each CORS headers create the specific response. | Cors |
getActionId() protected | Returns an action ID by converting Action::$uniqueId into an ID relative to the module. | yii\ |
headerize() protected | Convert any string (including php headers with HTTP prefix) to header format. | Cors |
headerizeToPhp() protected | Convert any string (including php headers with HTTP prefix) to header format. | Cors |
isActive() protected | Returns a value indicating whether the filter is active for the given action. | yii\ |
prepareAllowHeaders() protected | Handle classic CORS request to avoid duplicate code. | Cors |
Property details
Section titled “Property details”$request
Section titled “$request ”public yii\web\Request|null $request = nullThe current request.
If not set, the request application component will be used.
$response
Section titled “$response ”public yii\web\Response|null $response = nullThe response to be sent.
If not set, the response application component will be used.
Method details
Section titled “Method details”addCorsHeaders()
Section titled “addCorsHeaders() ”public function addCorsHeaders(yii\web\Response $response, array $headers): mixedAdds the CORS headers to the response.
Parameters
| Parameter | Type | Description |
|---|---|---|
$response | yii\ | |
$headers | array | CORS headers which have been computed |
Returns
mixed
beforeAction()
Section titled “beforeAction() ”public function beforeAction(mixed $action): boolThis method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.
Parameters
| Parameter | Type | Description |
|---|---|---|
$action | mixed | The action to be executed. |
Returns
bool: Whether the action should continue to be executed.
extractHeaders()
Section titled “extractHeaders() ”public function extractHeaders(): arrayExtract CORS headers from the request.
Returns
array: CORS headers to handle
overrideDefaultSettings()
Section titled “overrideDefaultSettings() ”public function overrideDefaultSettings(yii\base\Action $action): mixedOverride settings for specific action.
Parameters
| Parameter | Type | Description |
|---|---|---|
$action | yii\ | The action settings to override |
Returns
mixed
prepareHeaders()
Section titled “prepareHeaders() ”public function prepareHeaders(array $requestHeaders): arrayFor each CORS headers create the specific response.
Parameters
| Parameter | Type | Description |
|---|---|---|
$requestHeaders | array | CORS headers we have detected |
Returns
array: CORS headers ready to be sent
headerize()
Section titled “headerize() ”protected function headerize(string $string): stringConvert any string (including php headers with HTTP prefix) to header format.
Example:
- X-PINGOTHER -> X-Pingother
- X_PINGOTHER -> X-Pingother
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | String to convert |
Returns
string: The result in “header” format
headerizeToPhp()
Section titled “headerizeToPhp() ”protected function headerizeToPhp(string $string): stringConvert any string (including php headers with HTTP prefix) to header format.
Example:
- X-Pingother -> HTTP_X_PINGOTHER
- X PINGOTHER -> HTTP_X_PINGOTHER
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | String to convert |
Returns
string: The result in “php $_SERVER header” format
prepareAllowHeaders()
Section titled “prepareAllowHeaders() ”protected function prepareAllowHeaders(string $type, array $requestHeaders, array &$responseHeaders): mixedHandle classic CORS request to avoid duplicate code.
Parameters
| Parameter | Type | Description |
|---|---|---|
$type | string | The kind of headers we would handle |
$requestHeaders | array | CORS headers request by client |
$responseHeaders | array | CORS response headers sent to the client |
Returns
mixed