我刚刚在Laravel中继承了一个项目,它使用的是Dingo API。
该API返回的是诸如Federaci\u00f3n
之类的字符串,而不是Federación
。
所有字符串在数据库中均已正确UTF-8编码,因此问题出在服务器上。
控制器返回$this->response->paginator($articles, new ArticlesCollectionTransformer);
,在ArticlesCollectionTransformer
内,我为遇到问题的字符串放置了一个var_dump,并且在那里正确地对其进行了编码。
我还要去哪里?我想念什么?
控制器:
public function index(Request $request)
{
$limit = (0 === (int)$request->limit || $request->limit > self::PER_PAGE) ? self::PER_PAGE : $request->limit;
$articles = Article::with('author', 'category')
->approved()
->orderBy('published_at', 'desc')
->orderBy('featured', 'desc')
->paginate($limit);
return $this->
response->
paginator($articles, new ArticlesCollectionTransformer)->
// also tried:
// withHeader('Content-type', 'application/json; charset=utf-8');
}
变形金刚
class ArticlesCollectionTransformer extends TransformerAbstract
{
public function transform (Article $article)
{
// var_dump($article->title) ==> is correctly encoded
return [
'id' => (int) $article->id,
'category' => $article->category->name,
'author' => $article->author->name ?? ArticleAuthor::DEFAULT_AUTHOR_FOR_NEW_ARTICLES,
'display_order' => (int) $article->display_order,
'title' => $article->title,
'featured' => (bool) $article->featured,
'viewed_times' => (int) $article->viewed_times,
'last_update' => $article->updated_at->timestamp,
];
}
}
更多信息:
5.2.*
1.0.x@dev