我想将我的API的JSON响应编码为UTF-8,但每次做出响应时我都不想这样做:
return response()->json($res,200,['Content-type'=>'application/json;charset=utf-8'],JSON_UNESCAPED_UNICODE);
所以我考虑为所有API路由制作一个中间件,handle(...)
函数将是这样的:
public function handle($request, Closure $next) {
$response = $next($request);
$response->header('Content-type','application/json; charset=utf-8');
return $next($request);
}
问题在于它无法工作,我的响应的Content-type
头仍为application/json
而不是application/json; charset=utf-8
; 可能是因为json(...)
函数已经设置了一个Content-type
头,并且我无法覆盖它。
我该怎么办呢?
感谢您的帮助。
$request->header('key', 'default')
是一行中的 getter,你正在执行 NOP(无操作)。 - Kyslik$response = $next($request);
并将$request
替换为$response
,但头部没有改变。 - JacopoStanchi