前端post過來的數(shù)據(jù)比如說有n個(gè)值,但都被加密最后變成一個(gè)值:encrypt_data=xxx,后端中間件需解密encrypt_data后把值重新裝進(jìn)post里面,但webman不支持修改post的數(shù)據(jù),以前使用thinkphp是支持的,目前想到的辦法:
1、把解密的值復(fù)制給 request()->xxx = xxx ,控制器或模型中就可以調(diào)用了;感覺不是很科學(xué),不夠完美,控制器模型里面獲取值的地方都要改掉
2、直接修改request類,或重寫;感覺也不完美也麻煩,也擔(dān)心后續(xù)框架升級會有影響問題
原則是不喜歡修改框架本身自帶的功能,不知道還有哪些辦法
搜索答案中都不支持修改post get數(shù)據(jù)
<?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://wtbis.cn/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace support;
/**
* Class Request
* @package support
*/
class Request extends \Webman\Http\Request
{
//重寫獲取path用于域名路由
public function path()
{
if (!isset($this->_data['path'])) {
$uri = $this->uri();
if(config('domain.enable', false)){
//如果開啟了域名路由
$bind = config('domain.bind', []);
$domain = $this->host(true);
if(isset($bind[$domain])) {
$uri = '/' . $bind[$domain] . $uri;
}
}
$this->_data['path'] = (string)\parse_url($uri, PHP_URL_PATH);
}
return $this->_data['path'];
}
//添加setRoute方法,用于字段加解密后覆蓋post的方法
public function setRoute(array $data):void{
$this->_data['post'] = $data;
}
}
繼承\(zhòng)Webman\Http\Request,對你需要的方法進(jìn)行重寫,比我我發(fā)現(xiàn),我需要每個(gè)接口把傳過來的參數(shù)去除空格,然后我就把post 方法重寫了
/**
* $_POST.
* 對該方法進(jìn)行重寫,過濾空格
* @param string|null $name
* @param mixed|null $default
* @return mixed|null
*/
public function post($name = null, $default = null)
{
if (!isset($this->_data['post'])) {
$this->parsePost();
}
if (null === $name) {
$postData = [];
foreach ($this->_data['post'] as $item => $value) {
if (!is_array($value)) $value = trim($value);
$postData[$item] = $value;
}
return $postData;
}
return isset($this->_data['post'][$name]) ? trim($this->_data['post'][$name]) : $default;
}
你可以對他進(jìn)行修改,增加其他的操作
$currentBuffer = $request->rawBuffer();
POST /xx/xx HTTP/1.1
X-Real-IP: 127.0.0.1
Host: xxx.xx
X-Forwarded-Proto: http
Content-Length: 378
User-Agent: Apifox/1.0.0 (https://apifox.com)
Accept: */*
Accept-Encoding: gzip, deflate, br
Content-Type: multipart/form-data; boundary=--------------------------572433850403036298600651
----------------------------572433850403036298600651
Content-Disposition: form-data; name="name"
jhon
----------------------------572433850403036298600651
Content-Disposition: form-data; name="num"
100
----------------------------572433850403036298600651
Content-Disposition: form-data; name="code"
120.567
----------------------------572433850403036298600651--
//修改$currentBuffer
$newBuffer = processFunctiontName($currentBuffer);
$newRequest = new Request($newBuffer);