laravel跨域

1、使用composr安装barryvdh/laravel-cors.

$ composer require barryvdh/laravel-cors

2、把CorsServiceProvider添加到config/app.php providers 数组中:

BarryvdhCorsServiceProvider::class,

3、发布cors配置

php artisan vendor:publish --provider="BarryvdhCorsServiceProvider"

4、注册中间件

在$middlewareGroups数组中的api键里添加'cors'
protected $middlewareGroups = [
    'web' => [
        AppHttpMiddlewareEncryptCookies::class,
        IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
        IlluminateSessionMiddlewareStartSession::class,
        IlluminateViewMiddlewareShareErrorsFromSession::class,
        AppHttpMiddlewareVerifyCsrfToken::class,
        IlluminateRoutingMiddlewareSubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
        'cors'
    ],
];