PHPackages                             topthink/think-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. topthink/think-cors

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

topthink/think-cors
===================

The Cors Library For ThinkPHP

v1.0.2(2y ago)1511.5k↑341.1%3[1 issues](https://github.com/top-think/think-cors/issues)1Apache-2.0PHP

Since Oct 11Pushed 1y ago2 watchersCompare

[ Source](https://github.com/top-think/think-cors)[ Packagist](https://packagist.org/packages/topthink/think-cors)[ RSS](/packages/topthink-think-cors/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (1)

ThinkCors
=========

[](#thinkcors)

ThinkPHP跨域扩展

安装
--

[](#安装)

```
composer require topthink/think-cors

```

配置
--

[](#配置)

配置文件位于 `config/cors.php`

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

```

### paths 配置示例

[](#paths-配置示例)

允许 api 目录下的跨域请求，`*` 代表通配符。

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

当项目有多个域名时，支持为不同域名配置不同的目录。

```
[
    'paths' => [
        'www.thinkphp.cn' => ['api/*'],
        'doc.thinkphp.cn' => ['user/*', 'article/*'],
    ]
]
```

### allowed\_origins 配置示例

[](#allowed_origins-配置示例)

当配置中有 `*` 时，代表不限制来源域。

```
[
    'allowed_origins' => ['*'],
]
```

当我们需要限制来源域时，可以这么写。

```
[
    'allowed_origins' => ['www.thinkphp.cn', 'm.thinkphp.cn'],
]
```

### allowed\_origins\_patterns 配置示例

[](#allowed_origins_patterns-配置示例)

除了固定来源域，有时候我们还想要允许不固定但有规则的来源域，那么可以通过正则来实现。例如这里我们允许 `thinkphp.cn` 的所有二级域。

```
[
    'allowed_origins_patterns' => ['#.*\.thinkphp\.cn#'],
]
```

### allowed\_methods 配置示例

[](#allowed_methods-配置示例)

当配置中有 `*` 时，代表不限制来源请求方式。

```
[
    'allowed_methods' => ['*'],
]
```

当然我们也可以限制只允许 `GET` 和 `POST` 的跨域请求。

```
[
    'allowed_methods' => ['GET', 'POST'],
]
```

### allowed\_headers 配置示例

[](#allowed_headers-配置示例)

当配置中有 `*` 时，代表不限制请求头。

```
[
    'allowed_headers' => ['*'],
]
```

当然我们也可以只允许跨域请求只传递给我们部分请求头。

```
[
    'allowed_headers' => ['X-Custom-Header', 'Upgrade-Insecure-Requests'],
]
```

### max\_age 配置示例

[](#max_age-配置示例)

跨域预检结果是有缓存的，如果值为 -1，表示禁用缓存，则每次请求前都需要使用 `OPTIONS` 预检请求。如果想减少 `OPTIONS` 预检请求，我们可以把缓存有效期设置长些。 列如这里，我们把有效期设置为 2 小时（7200 秒）：

```
[
    'max_age' => 7200,
]
```

### supports\_credentials 配置示例

[](#supports_credentials-配置示例)

`Credentials` 可以是 `cookies`、`authorization headers` 或 `TLS client certificates`。当接口需要这些信息时，开启该项配置后，相关请求将会携带 `Credentials` 信息（如果有的话）。

```
[
    'supports_credentials' => true,
]
```

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance26

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~99 days

Total

3

Last Release

799d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7422243107bba2d58c5682b69d1c405e66fa027df2f6202afc1bb0c55559a7b1?d=identicon)[yunwuxin](/maintainers/yunwuxin)

---

Top Contributors

[![big-dream](https://avatars.githubusercontent.com/u/9215157?v=4)](https://github.com/big-dream "big-dream (2 commits)")[![qeq66](https://avatars.githubusercontent.com/u/19647183?v=4)](https://github.com/qeq66 "qeq66 (2 commits)")[![liu21st](https://avatars.githubusercontent.com/u/1111670?v=4)](https://github.com/liu21st "liu21st (1 commits)")[![yunwuxin](https://avatars.githubusercontent.com/u/2168125?v=4)](https://github.com/yunwuxin "yunwuxin (1 commits)")

### Embed Badge

![Health badge](/badges/topthink-think-cors/health.svg)

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

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25126.1M82](/packages/php-http-cache-plugin)[illuminate/http

The Illuminate Http package.

11937.9M6.9k](/packages/illuminate-http)[rdkafka/rdkafka

A PHP extension for Kafka

2.2k24.3k1](/packages/rdkafka-rdkafka)[mezzio/mezzio-router

Router subcomponent for Mezzio

265.4M92](/packages/mezzio-mezzio-router)[zoujingli/think-library

Core library and common services for ThinkAdmin

8373.4k20](/packages/zoujingli-think-library)[ichynul/tpextmyadmin

thinkphp extension

141.6k](/packages/ichynul-tpextmyadmin)

PHPackages © 2026

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