【发布时间】:2018-11-15 22:32:13
【问题描述】:
我想将我的 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 标头,我无法覆盖它。
我该怎么办?
感谢您的帮助。
【问题讨论】:
-
你需要换一种方式,你想使用 after middleware.
-
我不明白你的意思。
-
$request->header('key', 'default')是您正在执行 NOP(无操作)的那一行中的吸气剂。 -
你是对的。我添加了
$response = $next($request);并将$request替换为$response,但标题没有改变。
标签: json laravel utf-8 laravel-middleware