|
| 1 | +# CORS |
| 2 | + |
| 3 | +## What is CORS? |
| 4 | + |
| 5 | +**Cross-Origin Resource Sharing** or _CORS_ is an HTTP-header based mechanism that allows a server to indicate any other |
| 6 | +origins (domain, scheme, or port) than its own from which a browser should permit loading of resources. |
| 7 | + |
| 8 | +## Why do we need CORS? |
| 9 | + |
| 10 | +When integrating an API, most developers have encountered the following error message: |
| 11 | + |
| 12 | +> Access to fetch at _RESOURCE_URL_ from origin _ORIGIN_URL_ has been blocked by CORS policy: |
| 13 | +> No ‘Access-Control-Allow-Origin’ header is present on the requested resource. |
| 14 | +
|
| 15 | +This happens because the API (_RESOURCE_URL_) is not configured to accept requests from the client (_RESOURCE_URL_). |
| 16 | + |
| 17 | +## How to fix? |
| 18 | + |
| 19 | +DotKernel API fixes this issue using the [mezzio/mezzio-cors](https://github.com/mezzio/mezzio-cors) library. |
| 20 | + |
| 21 | +### Step 1: Install library |
| 22 | + |
| 23 | +In order to install `mezzio/mezzio-cors`, run the following command: |
| 24 | + |
| 25 | +```shell |
| 26 | +composer require mezzio/mezzio-cors |
| 27 | +``` |
| 28 | + |
| 29 | +### Step 2: Configure your API |
| 30 | + |
| 31 | +#### Register ConfigProvider |
| 32 | + |
| 33 | +Register `mezzio/mezzio-cors` in your application by adding its ConfigProvider to your application's config aggregator. |
| 34 | +Open the file `config/config.php` and paste the below lines at the beginning of the array passed to `ConfigAggregator`: |
| 35 | + |
| 36 | +```php |
| 37 | +Laminas\Diactoros\ConfigProvider::class, |
| 38 | +Mezzio\Cors\ConfigProvider::class, |
| 39 | +``` |
| 40 | + |
| 41 | +Save and close the file. |
| 42 | + |
| 43 | +#### Add middleware |
| 44 | + |
| 45 | +Add `mezzio/mezzio-cors` middleware to your application's pipeline. |
| 46 | +Open `config/pipeline.php` and paste the below line before the one with `RouteMiddleware::class`: |
| 47 | + |
| 48 | +```php |
| 49 | +$app->pipe(\Mezzio\Cors\Middleware\CorsMiddleware::class); |
| 50 | +``` |
| 51 | + |
| 52 | +Save and close the file. |
| 53 | + |
| 54 | +#### Create config file |
| 55 | + |
| 56 | +Create and open file `config/autoload/cors.local.php` and add the following code inside it: |
| 57 | + |
| 58 | +```php |
| 59 | +<?php |
| 60 | + |
| 61 | +declare(strict_types=1); |
| 62 | + |
| 63 | +use Mezzio\Cors\Configuration\ConfigurationInterface; |
| 64 | + |
| 65 | +return [ |
| 66 | + ConfigurationInterface::CONFIGURATION_IDENTIFIER => [ |
| 67 | + 'allowed_origins' => [ |
| 68 | + ConfigurationInterface::ANY_ORIGIN, |
| 69 | + ], |
| 70 | + 'allowed_headers' => ['Accept', 'Content-Type', 'Authorization'], |
| 71 | + 'allowed_max_age' => '600', |
| 72 | + 'credentials_allowed' => true, |
| 73 | + 'exposed_headers' => [], |
| 74 | + ], |
| 75 | +]; |
| 76 | +``` |
| 77 | + |
| 78 | +See below list that explains the above configuration values: |
| 79 | + |
| 80 | +- `allowed_origins`: an array of domains that are allowed to interact with the API |
| 81 | +(default `ConfigurationInterface::ANY_ORIGIN` which means that any domain can make requests to the API) |
| 82 | +- `allowed_headers`: an array of custom headers allowed |
| 83 | +- `allowed_max_age`: the maximum age, the preflight response may be cached by a client |
| 84 | +- `credentials_allowed`: if a request is allowed to pass cookies |
| 85 | +- `exposed_headers`: an array of headers which are being exposed by the endpoint |
| 86 | + |
| 87 | +Save and close the file. |
| 88 | + |
| 89 | +> On the **production** environment, make sure you allow only specific origins by adding them to the `allowed_origins` |
| 90 | +> array and removing the current value of `ConfigurationInterface::ANY_ORIGIN`. |
| 91 | +
|
| 92 | +For more info, see [mezzio/mezzio-cors documentation](https://docs.mezzio.dev/mezzio-cors/v1/middleware/#configuration). |
0 commit comments