接口返回數(shù)據(jù)用數(shù)組,打算在后置中間件中把數(shù)組轉(zhuǎn)換為json返回給前端,如何在后置中間件里獲取response主體內(nèi)容,并轉(zhuǎn)換為json?
嘗試用$response->rawBody
就想在中間件中實(shí)現(xiàn),不要推薦其它方案,如果需要其它方案,就不會在這提問了
RegController 接口代碼
namespace app\api\controller;
use support\Request;
class RegController{
public function reg(Request $req){
$param = [
'status'=>1
];
return showReturnCode(200,$param,'success'); //返回的是數(shù)組
}
}
后置中間件代碼
namespace app\api\middleware;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
class ResJsonMiddleware implements MiddlewareInterface {
public function process(Request $request, callable $handler): Response {
$response = $handler($request);
$rawBody = $response->rawBody();
print_r($rawBody); //只打印 Array 這幾個字符
return $response;
}
}