Laravel:以 UTF-8 编码 JSON 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50717005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: Encode JSON responses in UTF-8
提问by JacopoStanchi
I want to encode the JSON responses of my API to UTF-8, but every time I make a response I don't want to do this:
我想将我的 API 的 JSON 响应编码为 UTF-8,但每次我做出响应时我都不想这样做:
return response()->json($res,200,['Content-type'=>'application/json;charset=utf-8'],JSON_UNESCAPED_UNICODE);
So I thought about making a middleware for all API routes which handle(...)
function would be this:
所以我想为所有 API 路由制作一个中间件,它的handle(...)
功能是这样的:
public function handle($request, Closure $next) {
$response = $next($request);
$response->header('Content-type','application/json; charset=utf-8');
return $next($request);
}
The problem is that it doesn't work, the Content-type
header of my responses is still application/json
and not application/json; charset=utf-8
; maybe because the json(...)
function already sets a Content-type
header and I cannot override it.
问题是它不起作用Content-type
,我的回复的标题仍然application/json
不是application/json; charset=utf-8
;可能是因为该json(...)
函数已经设置了一个Content-type
标题,我无法覆盖它。
How should I do?
我应该怎么做?
Thank you for your help.
感谢您的帮助。
回答by Kyslik
Its right there in the documentation, you want to use aftermiddleware (following code is from top of my head and it should work):
它就在文档中,您想在中间件之后使用(以下代码来自我的头脑,它应该可以工作):
<?php
namespace App\Http\Middleware;
use Closure;
class AfterMiddleware
{
public function handle($request, Closure $next)
{
/** @var array $data */ // you need to return array from controller
$data = $next($request);
return response()->json($data, 200, ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
JSON_UNESCAPED_UNICODE);
}
}
With above approach we can spot two anti-patterns:
通过上述方法,我们可以发现两种反模式:
- crafting response in middleware (you should do it in controller)
- using unescaped JSON response, Laravel creators made default the escaped one so why change it?!
- 在中间件中制作响应(您应该在控制器中进行)
- 使用未转义的 JSON 响应,Laravel 创建者将转义的默认设置为默认值,那么为什么要更改它呢?!
Removing middleware and using controller only
删除中间件并仅使用控制器
Put following code in app/Http/Controller.php
将以下代码放入app/Http/Controller.php
protected function jsonResponse($data, $code = 200)
{
return response()->json($data, $code,
['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'], JSON_UNESCAPED_UNICODE);
}
in any of the controllers that are extended by base controller (app/Http/Controller.php) you can use $this->jsonResponse($data);
在任何由基本控制器 (app/Http/Controller.php) 扩展的控制器中,您可以使用 $this->jsonResponse($data);
How pros do it
高手是怎么做的
They use eloquent resourcesor if there is more going on fractalis the way to go (in Laravel use spatie wrapper - https://github.com/spatie/laravel-fractal).
他们使用雄辩的资源,或者如果有更多的分形是要走的路(在 Laravel 中使用 spatie 包装器 - https://github.com/spatie/laravel-fractal)。