laravel8 优雅解决跨域

目录

移除laravel8可能的老版本跨域解决方案,下载最新版

在配置文件app/Http/Kernal.php中引入

在config/cors.php文件中设置允许跨域的路径

老版本

运行composer update 更新

 在配置文件app/Http/Kernal.php中引入

在config文件夹下添加cors.php文件


原文链接:GitHub - fruitcake/laravel-cors: Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application

移除laravel8可能的老版本跨域解决方案,下载最新版

composer remove barryvdh/laravel-cors fruitcake/laravel-cors
composer require fruitcake/laravel-cors

第一条命令会执行的相对慢一点,大概4分钟吧,第二条命令执行起来就很快了

在配置文件app/Http/Kernal.php中引入

protected $middleware = [
  \Fruitcake\Cors\HandleCors::class,
    // ...
];

在config/cors.php文件中设置允许跨域的路径

 'paths' => ['api/*', 'sanctum/csrf-cookie'],

老版本

在composer.json中直接写入fruitcake/laravel-cors的版本

    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.6.*",
        "laravel/tinker": "^1.0",
        "fruitcake/laravel-cors": "^1.0"
    },

运行composer update 更新

 在配置文件app/Http/Kernal.php中引入

protected $middleware = [
  \Fruitcake\Cors\HandleCors::class,
    // ...
];

在config文件夹下添加cors.php文件,paths字段设置允许跨域的路径

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Laravel CORS Options
    |--------------------------------------------------------------------------
    |
    | The allowed_methods and allowed_headers options are case-insensitive.
    |
    | You don't need to provide both allowed_origins and allowed_origins_patterns.
    | If one of the strings passed matches, it is considered a valid origin.
    |
    | If array('*') is provided to allowed_methods, allowed_origins or allowed_headers
    | all methods / origins / headers are allowed.
    |
    */

    /*
     * You can enable CORS for 1 or multiple paths.
     * Example: ['api/*']
     */
    'paths' => ['*'],

    /*
    * Matches the request method. `[*]` allows all methods.
    */
    'allowed_methods' => ['*'],

    /*
     * Matches the request origin. `[*]` allows all origins.
     */
    'allowed_origins' => ['*'],

    /*
     * Matches the request origin with, similar to `Request::is()`
     */
    'allowed_origins_patterns' => [],

    /*
     * Sets the Access-Control-Allow-Headers response header. `[*]` allows all headers.
     */
    'allowed_headers' => ['*'],

    /*
     * Sets the Access-Control-Expose-Headers response header.
     */
    'exposed_headers' => false,

    /*
     * Sets the Access-Control-Max-Age response header.
     */
    'max_age' => false,

    /*
     * Sets the Access-Control-Allow-Credentials header.
     */
    'supports_credentials' => false,
];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值