PHPackages                             alejandro-fiore/zfr-cors - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. alejandro-fiore/zfr-cors

ActiveLibrary[HTTP &amp; Networking](/categories/http)

alejandro-fiore/zfr-cors
========================

Laminas module that let you deal with CORS requests

3.0.0(5y ago)0181MITPHPPHP ^7.1

Since Sep 8Pushed 5y agoCompare

[ Source](https://github.com/alejandro-fiore/zfr-cors)[ Packagist](https://packagist.org/packages/alejandro-fiore/zfr-cors)[ Docs](https://github.com/alejandro-fiore/zfr-cors)[ RSS](/packages/alejandro-fiore-zfr-cors/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (13)Versions (17)Used By (0)

ZfrCors
=======

[](#zfrcors)

[![Build Status](https://camo.githubusercontent.com/dfdf9d31cda178421e905a4451eaf7bd357757f8e82f45c58f9f10ee621ca270/68747470733a2f2f7472617669732d63692e6f72672f7a662d66722f7a66722d636f72732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/zf-fr/zfr-cors)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/85bb8529ca91b24a7f419f7b90f4f69de3a714355707151ba6916f0d0c3f6284/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7a662d66722f7a66722d636f72732f6261646765732f7175616c6974792d73636f72652e706e673f733d34373530346435663561303466383866623430616562626435323464396432343163326165353838)](https://scrutinizer-ci.com/g/zf-fr/zfr-cors/)[![Coverage Status](https://camo.githubusercontent.com/a356e6d82c69b572b812d7910de9d27c169e76eb9f9de586f05c42486bc6623d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f7a662d66722f7a66722d636f72732f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/zf-fr/zfr-cors?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/7f545b5870100e25496b72801b83b37e887d3d4795444fd199e21df0c3a2a556/68747470733a2f2f706f7365722e707567782e6f72672f7a66722f7a66722d636f72732f762f737461626c652e706e67)](https://packagist.org/packages/zfr/zfr-cors)

ZfrCors is a simple ZF2 module that helps you to deal with Cross-Origin Resource Sharing (CORS).

What is ZfrCors ?
-----------------

[](#what-is-zfrcors-)

ZfrCors is a Zend Framework 2 module that allow to easily configure your ZF 2 application so that it automatically builds HTTP responses that follow the CORS documentation.

### Installation

[](#installation)

Install the module by typing (or add it to your `composer.json` file):

```
$ php composer.phar require zfr/zfr-cors
```

Then, enable it by adding "ZfrCors" in your `application.config.php` file.

By default, ZfrCors is configured to deny every CORS requests. To change that, you need to copy the [`config/zfr_cors.global.php.dist`](config/zfr_cors.global.php.dist) file to your `autoload` folder (remove the `.dist` extension), and modify it to suit your needs.

Documentation
-------------

[](#documentation)

### What is CORS ?

[](#what-is-cors-)

CORS is a mechanism that allows to perform cross-origin requests from your browser.

For instance, let's say that your website is hosted in the domain `http://example.com`. By default, user agents won't be allowed to perform AJAX requests to another domain for security reasons (for instance `http://funny-domain.com`).

With CORS, you can allow your server to reply to such requests.

You can find better documentation on how CORS works on the web:

- [Mozilla documentation about CORS](https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS)
- [CORS server flowchart](http://www.html5rocks.com/static/images/cors_server_flowchart.png)

### Event registration

[](#event-registration)

ZfrCors registers the `ZfrCors\Mvc\CorsRequestListener` with the `MvcEvent::EVENT_ROUTE` event, with a priority of -1. This means that this listener is executed AFTER the route has been matched.

### Configuring the module

[](#configuring-the-module)

As by default, all the various options are set globally for all routes:

- `allowed_origins`: (array) List of allowed origins. To allow any origin, you can use the wildcard (`*`) character. If multiple origins are specified, ZfrCors will automatically check the `"Origin"` header's value, and only return the allowed domain (if any) in the `"Allow-Access-Control-Origin"` response header. To allow any sub-domain, you can prefix the domain with the wildcard character (i.e. `*.example.com`). Please note that you don't need to add your host URI (so if your website is hosted as "example.com", "example.com" is automatically allowed.
- `allowed_methods`: (array) List of allowed HTTP methods. Those methods will be returned for the preflight request to indicate which methods are allowed to the user agent. You can even specify custom HTTP verbs.
- `allowed_headers`: (array) List of allowed headers that will be returned for the preflight request. This indicates to the user agent which headers are permitted to be sent when doing the actual request.
- `max_age`: (int) Maximum age (seconds) the preflight request should be cached by the user agent. This prevents the user agent from sending a preflight request for each request.
- `exposed_headers`: (array) List of response headers that are allowed to be read in the user agent. Please note that some browsers do not implement this feature correctly.
- `allowed_credentials`: (boolean) If true, it allows the browser to send cookies along with the request.

If you want to configure specific routes, you can add `ZfrCors\Options\CorsOptions::ROUTE_PARAM` to your route configuration:

```
