乐趣区

关于php:tp6-通过全局中间件-解决跨域问题

tp6 官网有提供跨域决绝办法,当我间接应用无奈用。(可能我用的姿态不对)。

定义中间件


<?php
declare (strict_types = 1);

namespace app\middleware;
use think\Response;

/**
 * 全局跨域申请解决
 * Class CrossDomain
 * @package app\middleware
 */

class CrossDomain
{public function handle($request, \Closure $next)
    {header('Access-Control-Allow-Origin: *');
        header('Access-Control-Max-Age: 1800');
        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
        header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With, Token');
        if (strtoupper($request->method()) == "OPTIONS") {return Response::create()->send();}

        return $next($request);
    }
}

在 middleware.php 中退出咱们定义的中间件

而后跨域就好使了!

退出移动版