ThinkPHP6 开起跨域

1585364631
2023-02-15 / 0 评论 / 294 阅读 / 正在检测是否收录...

ThinkPHP6 开启接口跨域

1.新建中间件

<?php

namespace app\home\middleware;

use Closure;
use think\middleware\AllowCrossDomain;
use think\Request;
use think\Response;
use think\response\Redirect;

class Allow extends AllowCrossDomain
{
    protected $header = [
        'Access-Control-Allow-Credentials' => 'true',
        'Access-Control-Max-Age'           => 1800,
        'Access-Control-Allow-Methods'     => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers'     => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With, jwtToken',
    ];
}

2.全局使用中间件或者路由使用中间件

app/middleware.php
<?php
// 全局中间件定义文件
return [
    // 加上这句
    \app\home\middleware\Allow::class,
];
0

评论 (0)

取消