PHPackages                             agungsugiarto/codeigniter4-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. agungsugiarto/codeigniter4-cors

ActiveLibrary[API Development](/categories/api)

agungsugiarto/codeigniter4-cors
===============================

Send CORS Headers in a CodeIgniter 4 application.

v3.0.0(3y ago)6525.8k↓51.2%14[1 issues](https://github.com/agungsugiarto/codeigniter4-cors/issues)[2 PRs](https://github.com/agungsugiarto/codeigniter4-cors/pulls)2MITPHPPHP ^7.4 || ^8.0CI failing

Since Aug 2Pushed 3y ago4 watchersCompare

[ Source](https://github.com/agungsugiarto/codeigniter4-cors)[ Packagist](https://packagist.org/packages/agungsugiarto/codeigniter4-cors)[ Fund](https://saweria.co/agungsugiarto)[ RSS](/packages/agungsugiarto-codeigniter4-cors/feed)WikiDiscussions 3.x Synced 2d ago

READMEChangelog (9)Dependencies (2)Versions (13)Used By (2)

CORS Filter for CodeIgniter 4
=============================

[](#cors-filter-for-codeigniter-4)

Inspired from  for CodeIgniter 4

[![Latest Stable Version](https://camo.githubusercontent.com/43b5fbdd0e92f522d49a40c932e665948f78e49dcf50745871c5a7b6ace2fcea/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d636f72732f76)](https://packagist.org/packages/agungsugiarto/codeigniter4-cors)[![Total Downloads](https://camo.githubusercontent.com/7bb08761a7779ffe1adc6870ef797cbb07632cd67abea2e874f1773e8f13f72c/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d636f72732f646f776e6c6f616473)](https://packagist.org/packages/agungsugiarto/codeigniter4-cors)[![Latest Unstable Version](https://camo.githubusercontent.com/92f3db539db4e4f69f1d833c9868cd22a0fff0c726adb826930867c1a72e44ae/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d636f72732f762f756e737461626c65)](https://packagist.org/packages/agungsugiarto/codeigniter4-cors)[![License](https://camo.githubusercontent.com/3f589f920b3768dc4672ceef667eaeb502c35af22331c575dc28287335d627f7/68747470733a2f2f706f7365722e707567782e6f72672f6167756e67737567696172746f2f636f646569676e69746572342d636f72732f6c6963656e7365)](https://packagist.org/packages/agungsugiarto/codeigniter4-cors)

**About**
---------

[](#about)

The `codeigniter4-cors` package allows you to send [Cross-Origin Resource Sharing](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/)headers with Codeigniter4 filter configuration.

**Features**
------------

[](#features)

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

**Upgrade from 2.x to v3.x**
----------------------------

[](#upgrade-from-2x-to-v3x)

Upgrade from version 2.x to 3.x. Open your `composer.json` find `agungsugiarto/codeigniter4-cors` and change value to `^3.0`

**Installation**
----------------

[](#installation)

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

```
composer require agungsugiarto/codeigniter4-cors
```

**Global usage**
----------------

[](#global-usage)

To allow CORS for all your routes, first register `CorsFilter.php` filter at the top of the `$aliases` property of `App/Config/Filter.php` class:

```
public $aliases = [
    'cors' => \Fluent\Cors\Filters\CorsFilter::class,
    // ...
];
```

### **Global restrictions**

[](#global-restrictions)

Restrict routes based on their URI pattern by editing **app/Config/Filters.php** and adding them to the `$filters` array, e.g.:

```
public $filters = [
    // ...
    'cors' => [
        'before' => ['api/*'],
        'after' => ['api/*']
    ],
];
```

### **Restricting a single route**

[](#restricting-a-single-route)

Any single route can be restricted by adding the filter option to the last parameter in any of the route definition methods:

```
$routes->get('api/users', 'UserController::index', ['filter' => 'cors']);
```

### **Restricting Route Groups**

[](#restricting-route-groups)

In the same way, entire groups of routes can be restricted within the `group()` method:

```
$routes->group('api/v1', ['filter' => 'cors'], function ($routes) {
    // ...
});
```

**Configuration**
-----------------

[](#configuration)

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

```
php spark cors:publish
```

> **Note:** When using custom headers, like `X-Auth-Token` or `X-Requested-With`, you must set the `allowedHeaders` 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 valueallowedOriginsMatches the request origin. Wildcards can be used, eg. `*.mydomain.com``['*']`allowedOriginsPatternsMatches the request origin with preg\_match.`[]`allowedMethodsMatches the request method.`['*']`allowedHeadersSets the Access-Control-Allow-Headers response header.`['*']`exposedHeadersSets the Access-Control-Expose-Headers response header.`false`maxAgeSets the Access-Control-Max-Age response header.`0`supportsCredentialsSets the Access-Control-Allow-Credentials header.`false``allowedOrigins`, `allowedHeaders` and `allowedMethods` can be set to `['*']` to accept any value.

> **Note:** For `allowedOrigins` 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 a specific as possible. You can start developing with loose constraints, but it's better to be as strict as possible!

**License**
-----------

[](#license)

Released under the MIT License, see [LICENSE](https://github.com/agungsugiarto/codeigniter4-cors/blob/master/LICENSE.md).

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~84 days

Recently: every ~186 days

Total

12

Last Release

1232d ago

Major Versions

1.x-dev → 2.x-dev2021-02-19

v2.0.0 → 3.x-dev2023-02-18

PHP version history (3 changes)v1.0.0PHP ^7.2

v1.1.1PHP ^7.3 || ^8.0

3.x-devPHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/d4650dfd30af25f75c8de8060b618e9773614a818000afeb21a9346c73955167?d=identicon)[agungsugiarto](/maintainers/agungsugiarto)

---

Top Contributors

[![agungsugiarto](https://avatars.githubusercontent.com/u/10989147?v=4)](https://github.com/agungsugiarto "agungsugiarto (30 commits)")

---

Tags

codeigniter4codeigniter4-librarycorsrequestapicorsajaxcodeigniter4codeigniter4-cors

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/agungsugiarto-codeigniter4-cors/health.svg)

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

###  Alternatives

[nelmio/cors-bundle

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

1.9k100.6M191](/packages/nelmio-cors-bundle)[pubnub/pubnub

This is the official PubNub PHP SDK repository.

1335.4M17](/packages/pubnub-pubnub)

PHPackages © 2026

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