PHPackages                             dnsoft/laravel-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. [API Development](/categories/api)
4. /
5. dnsoft/laravel-cors

ActiveLibrary[API Development](/categories/api)

dnsoft/laravel-cors
===================

Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application

v1.0.3(2y ago)09MITPHPPHP ^7.4|^8.0|^8.1

Since Jul 4Pushed 2y agoCompare

[ Source](https://github.com/nguyen-van-dong/laravel-cors)[ Packagist](https://packagist.org/packages/dnsoft/laravel-cors)[ Fund](https://fruitcake.nl)[ GitHub Sponsors](https://github.com/barryvdh)[ RSS](/packages/dnsoft-laravel-cors/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

CORS Middleware for Laravel
===========================

[](#cors-middleware-for-laravel)

[![Build Status](https://github.com/fruitcake/laravel-cors/actions/workflows/run-tests.yml/badge.svg)](https://github.com/fruitcake/laravel-cors/actions)[![Latest Stable Version](https://camo.githubusercontent.com/b2cd3e51b3d3f198a7dbb25c5b7591855b031fb878f41b91ac602822884ce3ee/68747470733a2f2f706f7365722e707567782e6f72672f667275697463616b652f6c61726176656c2d636f72732f76657273696f6e2e706e67)](https://packagist.org/packages/fruitcake/laravel-cors)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/df6f92cf998d9cef9241af9190a17a9bcd5cb310faebad1d6777b876e0a34935/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f667275697463616b652f6c61726176656c2d636f72732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fruitcake/laravel-cors)[![Fruitcake](https://camo.githubusercontent.com/28d7584b52e33d7b4ffb6b1bef8b89b6e598adb45c8c0d5f80349c1c304f385e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f776572656425323042792d467275697463616b652d6232626333352e737667)](https://fruitcake.nl/)

Implements  for Laravel

Note for users upgrading to Laravel 9, 10 or higher
---------------------------------------------------

[](#note-for-users-upgrading-to-laravel-9-10-or-higher)

### This package is deprecated because all supported Laravel versions now include the CORS middleware in the core.

[](#this-package-is-deprecated-because-all-supported-laravel-versions-now-include-the-cors-middleware-in-the-core)

Since Laravel 9.2, this Middleware is included in laravel/framework. You can use the provided middleware, which should be compatible with the Middleware and config provided in this package. See  for the changes.

Steps to upgrade:

1. Remove `"fruitcake/laravel-cors"` from your composer.json
2. Replace `\Fruitcake\Cors\HandleCors::class,` with `\Illuminate\Http\Middleware\HandleCors::class,` in `app/Http/Kernel.php`

See `https://github.com/fruitcake/php-cors` for advanced usage. The config stays the same.

About
-----

[](#about)

The `laravel-cors` package allows you to send [Cross-Origin Resource Sharing](http://enable-cors.org/)headers with Laravel middleware configuration.

If you want to have a global overview of CORS workflow, you can browse this [image](http://www.html5rocks.com/static/images/cors_server_flowchart.png).

Upgrading from 0.x (barryvdh/laravel-cors)
------------------------------------------

[](#upgrading-from-0x-barryvdhlaravel-cors)

When upgrading from 0.x versions, there are some breaking changes:

- **A new 'paths' property is used to enable/disable CORS on certain routes. This is empty by default, so fill it correctly!**
- **Group middleware is no longer supported, use the global middleware**
- The vendor name has changed (see installation/usage)
- The casing on the props in `cors.php` has changed from camelCase to snake\_case, so if you already have a `cors.php` file you will need to update the props in there to match the new casing.

Features
--------

[](#features)

- Handles CORS pre-flight OPTIONS requests
- Adds CORS headers to your responses
- Match routes to only add CORS to certain Requests

Installation
------------

[](#installation)

Require the `fruitcake/laravel-cors` package in your `composer.json` and update your dependencies:

```
composer require fruitcake/laravel-cors
```

If you get a conflict, this could be because an older version of barryvdh/laravel-cors or fruitcake/laravel-cors is installed. Remove the conflicting package first, then try install again:

```
composer remove barryvdh/laravel-cors fruitcake/laravel-cors
composer require fruitcake/laravel-cors
```

Global usage
------------

[](#global-usage)

To allow CORS for all your routes, add the `HandleCors` middleware at the top of the `$middleware` property of `app/Http/Kernel.php` class:

```
protected $middleware = [
  \Fruitcake\Cors\HandleCors::class,
    // ...
];
```

Now update the config to define the paths you want to run the CORS service on, (see Configuration below):

```
'paths' => ['api/*'],
```

Configuration
-------------

[](#configuration)

The defaults are set in `config/cors.php`. Publish the config to copy the file to your own config:

```
php artisan vendor:publish --tag="cors"
```

> **Note:** When using custom headers, like `X-Auth-Token` or `X-Requested-With`, you must set the `allowed_headers` to include those headers. You can also set it to `['*']` to allow all custom headers.

> **Note:** If you are explicitly whitelisting headers, you must include `Origin` or requests will fail to be recognized as CORS.

### Options

[](#options)

OptionDescriptionDefault valuepathsYou can enable CORS for 1 or multiple paths, eg. `['api/*'] ``[]`allowed\_methodsMatches the request method.`['*']`allowed\_originsMatches the request origin. Wildcards can be used, eg. `*.mydomain.com` or `mydomain.com:*``['*']`allowed\_origins\_patternsMatches the request origin with `preg_match`.`[]`allowed\_headersSets the Access-Control-Allow-Headers response header.`['*']`exposed\_headersSets the Access-Control-Expose-Headers response header.`[]`max\_ageSets the Access-Control-Max-Age response header.`0`supports\_credentialsSets the Access-Control-Allow-Credentials header.`false``allowed_origins`, `allowed_headers` and `allowed_methods` can be set to `['*']` to accept any value.

> **Note:** For `allowed_origins` you must include the scheme when not using a wildcard, eg. `['http://example.com', 'https://example.com']`. You must also take into account that the scheme will be present when using `allowed_origins_patterns`.

> **Note:** Try to be as specific as possible. You can start developing with loose constraints, but it's better to be as strict as possible!

> **Note:** Because of [http method overriding](http://symfony.com/doc/current/reference/configuration/framework.html#http-method-override) in Laravel, allowing POST methods will also enable the API users to perform PUT and DELETE requests as well.

> **Note:** Sometimes it's necessary to specify the port *(when you're coding your app in a local environment for example)*. You can specify the port or using a wildcard here too, eg. `localhost:3000`, `localhost:*` or even using a FQDN `app.mydomain.com:8080`

### Lumen

[](#lumen)

On Lumen, just register the ServiceProvider manually in your `bootstrap/app.php` file:

```
$app->register(Fruitcake\Cors\CorsServiceProvider::class);
```

Also copy the [cors.php](https://github.com/fruitcake/laravel-cors/blob/master/config/cors.php) config file to `config/cors.php` and put it into action:

```
$app->configure('cors');
```

Global usage for Lumen
----------------------

[](#global-usage-for-lumen)

To allow CORS for all your routes, add the `HandleCors` middleware to the global middleware and set the `paths` property in the config.

```
$app->middleware([
    // ...
    Fruitcake\Cors\HandleCors::class,
]);
```

Common problems
---------------

[](#common-problems)

### Wrong config

[](#wrong-config)

Make sure the `path` option in the config is correct and actually matches the route you are using. Remember to clear the config cache as well.

### Error handling, Middleware order

[](#error-handling-middleware-order)

Sometimes errors/middleware that return own responses can prevent the CORS Middleware from being run. Try changing the order of the Middleware and make sure it's the first entry in the global middleware, not a route group. Also check your logs for actual errors, because without CORS, the errors will be swallowed by the browser, only showing CORS errors. Also try running it without CORS to make sure it actually works.

### Authorization headers / Credentials

[](#authorization-headers--credentials)

If your Request includes an Authorization header or uses Credentials mode, set the `supports_credentials` value in the config to true. This will set the [Access-Control-Allow-Credentials](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials) Header to `true`.

### Echo/die

[](#echodie)

If you use `echo()`, `dd()`, `die()`, `exit()`, `dump()` etc in your code, you will break the Middleware flow. When output is sent before headers, CORS cannot be added. When the script exits before the CORS middleware finishes, CORS headers will not be added. Always return a proper response or throw an Exception.

### Disabling CSRF protection for your API

[](#disabling-csrf-protection-for-your-api)

If possible, use a route group with CSRF protection disabled. Otherwise you can disable CSRF for certain requests in `App\Http\Middleware\VerifyCsrfToken`:

```
protected $except = [
    'api/*',
    'sub.domain.zone' => [
      'prefix/*'
    ],
];
```

### Duplicate headers

[](#duplicate-headers)

The CORS Middleware should be the only place you add these headers. If you also add headers in .htaccess, nginx or your index.php file, you will get duplicate headers and unexpected results.

### No Cross-Site requests

[](#no-cross-site-requests)

If you are not doing Cross-Site requests, meaning if you are not requesting site-a.com/api from site-b.com, your browser will not send the `Origin: https://site-b.com` request header, CORS will be "disabled" as the `Access-Control-Allow-Origin` header will be also missing. This happens because requests are being dispatched from the same and no protection is needed in this case.

License
-------

[](#license)

Released under the MIT License, see [LICENSE](LICENSE).

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

1040d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1554f58ee86bc89d66b752920f5f5487a73d4801793c56896785540fe149884a?d=identicon)[Dong Nguyen](/maintainers/Dong%20Nguyen)

---

Top Contributors

[![barryvdh](https://avatars.githubusercontent.com/u/973269?v=4)](https://github.com/barryvdh "barryvdh (265 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (11 commits)")[![nguyen-van-dong](https://avatars.githubusercontent.com/u/67553313?v=4)](https://github.com/nguyen-van-dong "nguyen-van-dong (5 commits)")[![joaorobertopb](https://avatars.githubusercontent.com/u/6556083?v=4)](https://github.com/joaorobertopb "joaorobertopb (4 commits)")[![adamwathan](https://avatars.githubusercontent.com/u/4323180?v=4)](https://github.com/adamwathan "adamwathan (2 commits)")[![binhqx](https://avatars.githubusercontent.com/u/582192?v=4)](https://github.com/binhqx "binhqx (2 commits)")[![cozylife](https://avatars.githubusercontent.com/u/81341241?v=4)](https://github.com/cozylife "cozylife (2 commits)")[![honeroku](https://avatars.githubusercontent.com/u/1676856?v=4)](https://github.com/honeroku "honeroku (2 commits)")[![jasonmccreary](https://avatars.githubusercontent.com/u/161071?v=4)](https://github.com/jasonmccreary "jasonmccreary (2 commits)")[![jfexyz](https://avatars.githubusercontent.com/u/66879?v=4)](https://github.com/jfexyz "jfexyz (2 commits)")[![Krisell](https://avatars.githubusercontent.com/u/25909128?v=4)](https://github.com/Krisell "Krisell (2 commits)")[![whoan](https://avatars.githubusercontent.com/u/7103003?v=4)](https://github.com/whoan "whoan (2 commits)")[![codeclown](https://avatars.githubusercontent.com/u/211543?v=4)](https://github.com/codeclown "codeclown (1 commits)")[![Wolg](https://avatars.githubusercontent.com/u/538630?v=4)](https://github.com/Wolg "Wolg (1 commits)")[![marktinsley](https://avatars.githubusercontent.com/u/303598?v=4)](https://github.com/marktinsley "marktinsley (1 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (1 commits)")[![nmokkenstorm](https://avatars.githubusercontent.com/u/33529698?v=4)](https://github.com/nmokkenstorm "nmokkenstorm (1 commits)")[![nyanlynnhtut](https://avatars.githubusercontent.com/u/1456363?v=4)](https://github.com/nyanlynnhtut "nyanlynnhtut (1 commits)")[![omarkdev](https://avatars.githubusercontent.com/u/14936241?v=4)](https://github.com/omarkdev "omarkdev (1 commits)")[![PaolaRuby](https://avatars.githubusercontent.com/u/79208489?v=4)](https://github.com/PaolaRuby "PaolaRuby (1 commits)")

---

Tags

apilaravelcorscrossdomain

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/dnsoft-laravel-cors/health.svg)

```
[![Health](https://phpackages.com/badges/dnsoft-laravel-cors/health.svg)](https://phpackages.com/packages/dnsoft-laravel-cors)
```

###  Alternatives

[nelmio/cors-bundle

Adds CORS (Cross-Origin Resource Sharing) headers support in your Symfony application

1.9k94.4M152](/packages/nelmio-cors-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
