Giter Site home page Giter Site logo

think-cors's Introduction

ThinkCors

ThinkPHP跨域扩展

安装

composer require topthink/think-cors

配置

配置文件位于 config/cors.php

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

paths 配置示例

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

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

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

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

allowed_origins 配置示例

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

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

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

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

allowed_origins_patterns 配置示例

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

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

allowed_methods 配置示例

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

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

当然我们也可以限制只允许 GETPOST 的跨域请求。

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

allowed_headers 配置示例

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

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

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

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

max_age 配置示例

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

[
    'max_age' => 7200,
]

supports_credentials 配置示例

Credentials 可以是 cookiesauthorization headersTLS client certificates。当接口需要这些信息时,开启该项配置后,相关请求将会携带 Credentials 信息(如果有的话)。

[
    'supports_credentials' => true,
]

think-cors's People

Contributors

qeq66 avatar yunwuxin avatar big-dream avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.