php Laravel 5.6 CORS中间件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel 5.6 CORS中间件相关的知识,希望对你有一定的参考价值。

// Full File Path: app/Http/Kernel.php

protected $middleware = [
    [...]
    \App\Http\Middleware\CORS::class
];

protected $routeMiddleware = [
    [...]
    'cors' => \App\Http\Middleware\CORS::class
];
// Full File Path: app/Http/Middleware/CORS.php

<?php

namespace App\Http\Middleware;

use Closure;


class CORS {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        $response = $next($request);

        $response->headers->set('Access-Control-Allow-Origin' , '*');
        $response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
        $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');

        return $response;
    }
}

以上是关于php Laravel 5.6 CORS中间件的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5.6 中的“绑定”中间件有啥作用?

barryvdh/laravel-cors 配置在 Laravel 5.6 中不起作用;忽略'allowedMethods'

laravel7.0 Laravel-cors 开发包实现跨域

在laravel 5.6的cors与护照和vues js与axios

Laravel Cors 中间件不适用于 POST 请求

Angular IO 与 Laravel Lumen PHP API CORS 问题