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();

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

复制代码
return [
    '' => [
        app\middleware\CrossDomain::class,//跨域请求
    ]
];

CrossDomain中间件的代码

复制代码
<?php

namespace app\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

//跨域
class  CrossDomain implements MiddlewareInterface
{
    public function process(Request $request, callable $next): Response
    {
//        p(getTime() . self::class);
        // 如果是options请求则返回一个空响应,否则继续向洋葱芯穿越,并得到一个响应
        $response = strtoupper($request->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;
    }
}

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

复制代码
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
]);

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

复制代码
<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @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));
    }
}
相关推荐
该用户已不存在16 小时前
Symfony AI v0.2.0 正式发布:功能解读与实战指南
php·ai编程·symfony
网安CILLE16 小时前
PHP四大输出语句
linux·开发语言·python·web安全·网络安全·系统安全·php
小小代码狗20 小时前
VS中配置php的保姆级教程
vscode·php
Arwen30321 小时前
如何消除APP、软件的不安全下载提示?怎样快速申请代码签名证书?
网络·网络协议·tcp/ip·安全·php·ssl
m0_738120721 天前
应急响应——知攻善防蓝队溯源靶机Linux-2详细流程
linux·服务器·网络·安全·web安全·php
oMcLin1 天前
如何在Ubuntu 22.04上通过调优Nginx和PHP‑FPM,提升高并发WordPress网站的负载均衡与缓存性能?
nginx·ubuntu·php
全栈小51 天前
【PHP】如何将ThinkPHP 5部署到windows服务器的IIS里,和PHP版本又是一个怎么样的关系,三分钟教程搞定部署
服务器·windows·php
JaguarJack1 天前
2026 年 PHP 8.4 依然重要:跳到 8.5 之前你该掌握的特性
后端·php·服务端
BingoGo1 天前
2026 年 PHP 8.4 依然重要:跳到 8.5 之前你该掌握的特性
后端·php
专注前端30年2 天前
【PHP开发与安全防护实战】性能调优手册
android·安全·php