tp6跨域访问最新写法

发布时间 2023-03-27 17:26:52作者: newmiracle宇宙

tp6跨域访问最新写法

   public function handle($request, Closure $next, ? array $header = [])
    {
    
       
        $header = !empty($header) ? array_merge($this->header, $header) : $this->header;

        if (!isset($header['Access-Control-Allow-Origin'])) {
            $origin = $request->header('origin');

            if ($origin && ('' == $this->cookieDomain || strpos($origin, $this->cookieDomain))) {
                $header['Access-Control-Allow-Origin'] = $origin;
            } else {
                $header['Access-Control-Allow-Origin'] = '*';
            }
        }
        if ($request->method() == 'OPTIONS') {
            $response = response('');
        } else {
            $response = $next($request);
        }
  
     
        return $response->header($header);
    }