• webman跨域相关问题


    2023年9月13日14:14:05

    webman版本1.5
    php版本8.0
    运行环境windows

    测试项目:https://gitee.com/open-php/zx-webman-website

    webman在跨域的时候,会有点不同因为第一个区别就是是否关闭自动路由

    //关闭自动路由
    Route::disableDefaultRoute();
    
    • 1
    • 2

    如果不关闭路由只要简单的在路由上挂上跨域中间件,如果开启自动路由,就在config/middleware.php添加就可以了

    return [
        '' => [
            app\middleware\CrossDomain::class,//跨域请求
        ]
    ];
    
    • 1
    • 2
    • 3
    • 4
    • 5

    CrossDomain中间件的代码

    method()) === 'OPTIONS' ? response('', 204) : $next($request);
            // 给响应添加跨域相关的http头
            $response->withHeaders([
                'Access-Control-Allow-Credentials' => 'true',
                'Access-Control-Allow-Origin' => $request->header('origin', '*'),
                'Access-Control-Allow-Methods' => $request->header('access-control-request-method', '*'),
                'Access-Control-Allow-Headers' => $request->header('access-control-request-headers', '*'),
            ]);
            return $response;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    如果关闭自动路由挂在中间件就如下:

    Route::group('/open', function () {
    
        Route::get('/test', [app\controller\Web\TestController::class, 'index'])->name('测试');
        Route::post('/uploadPic', [app\controller\Web\IndexController::class, 'uploadPic']);//上传图片文件
        Route::post('/uploadFile', [app\controller\Web\IndexController::class, 'uploadFile']);//上传普通文件
    })->middleware([
        app\middleware\CrossDomain::class,
        app\middleware\ApiLog::class
    ]);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    关闭自动路由的情况下需要额外配置一点东西

    
     * @copyright walkor
     * @link      http://www.workerman.net/
     * @license   http://www.opensource.org/licenses/mit-license.php MIT License
     */
    
    use app\util\GlobalCode;
    use Webman\Route;
    use support\Request;
    
    //请求不存在的url返回信息
    Route::fallback(function (Request $request) {
        $response = strtoupper($request->method()) === 'OPTIONS' ? response('', 204) : returnJson([GlobalCode::CODE => GlobalCode::NOT_FOUND, GlobalCode::MSG => '404 not found', GlobalCode::DATA => null]);
        $response->withHeaders([
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Allow-Origin' => "*",
            'Access-Control-Allow-Methods' => '*',
            'Access-Control-Allow-Headers' => '*',
        ]);
        return $response;
    });
    //关闭自动路由
    Route::disableDefaultRoute();
    
    //首页
    Route::get('/', function ($rquest) {
        return view('index/view');
    });
    
    //前台,api有权限
    Route::group('/open', function () {
    
        Route::get('/test', [app\controller\Web\TestController::class, 'index'])->name('测试');
        Route::post('/uploadPic', [app\controller\Web\IndexController::class, 'uploadPic']);//上传图片文件
        Route::post('/uploadFile', [app\controller\Web\IndexController::class, 'uploadFile']);//上传普通文件
    })->middleware([
        app\middleware\CrossDomain::class,
        app\middleware\ApiLog::class
    ]);
    
    
    if (!function_exists('returnJson')) {
        function returnJson(mixed $data = null, int $status = 200, array $headers = ['Content-Type' => 'application/json'], int $options = JSON_UNESCAPED_UNICODE): Response
        {
            return new Response($status, $headers, json_encode($data, $options));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
  • 相关阅读:
    springcloud3 分布式事务-seata的四种模式总结以及异地容灾
    计算机毕业设计php+vue基于微信小程序的在线挂号预约小程序
    Hive的基本知识
    最新总结MySQL核心知识点
    数据结构·单链表
    NTU 课程笔记:向量和矩阵
    视频流远程控制启动教程
    Word docx转html和markdown
    div做一个简单的自适应布局
    2022ICPC 网络赛第二场 F Infinity Tree
  • 原文地址:https://blog.csdn.net/zh7314/article/details/132962817