ajiho/think-cors

thinkphp6 的 cors 中间件

V1.0.3 2024-01-18 15:26 UTC

This package is auto-updated.

Last update: 2024-09-20 09:39:03 UTC


README

是一个简单的中间件,用于解决前后端分离时,在 thinkphp6 输出 API 时产生的跨域问题。

安装

composer require ajiho/think-cors

优点

尽管代码量不多,且很简单,但通过 composer 方式抽离出来安装,并通过配置文件的方式解决跨域问题变得非常简单。这样,多个项目可以复用,同时后期维护也比较方便。

配置

/config/cors.php

<?php
return [

    //允许的域名
    'allow_origin' => [
        '*'
    ],

    //允许的请求头
    "allow_headers" => [
        'Origin',
        'X-Requested-With',
        'Content-Type',
        'Accept',
        'Authorization',
        'Cache-Control'
    ],

    //允许的方法
    "allow_methods" => [
        'GET',
        'POST',
        'PUT',
        'DELETE',
        'OPTIONS',
        'PATCH'
    ]
];

使用

与 tp6 中间件用法相同

\ajiho\middleware\Cors::class