0

I've just inherited a project in Laravel and it's using Dingo API. The API is returning strings like Federaci\u00f3ninstead of Federación.

All strings are correctly UTF-8 encoded in the database, so the problem is in the server.

The controller is returning $this->response->paginator($articles, new ArticlesCollectionTransformer); and inside ArticlesCollectionTransformer I placed a var_dump for the strings I have issues with and they are correctly encoded there.

Where else should I look? What am I missing?

Controller:

 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');
    }

Transformer

    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,
        ];
    }
}

More info:

  • Laravel: 5.2.*
  • dingo/api: 1.0.x@dev
Rosana Rufer
  • 599
  • 8
  • 15
  • Take a look at this: https://stackoverflow.com/questions/24415284/laravel-cant-set-json-utf-8-charset I think the issue is that the JSON response isn't correct. Also you said you inherited the project. What version of Laravel is it running? – Petay87 Jan 08 '19 at 09:17
  • @Peta87 It is running `5.2.*` I'll update the question – Rosana Rufer Jan 08 '19 at 09:19
  • Can you show the controller function used to return the response and also the output from the var_dump? – Petay87 Jan 08 '19 at 09:33
  • @Peta87 Updated, but I don't think it's going to give you much informaition. I'm on larachat #questions if you want more real time feedback. Thank you so much for your time until now anyways. – Rosana Rufer Jan 08 '19 at 09:46
  • Can you dump it straight from laravel (without the dingo api) to narrow down the issue? – Chris Jan 08 '19 at 09:47
  • @Chris Yeah..I may need to get rid of this dependency but I wanted to know how to configure this part of dingo/API first. Will try what you say – Rosana Rufer Jan 08 '19 at 09:49
  • Oh na na, don't get rid of it! Just remove/disable it for a moment to see what happens. – Chris Jan 08 '19 at 10:01
  • Although you added the header to inform it of utf-8 encoding did you add the JSON_UNESCAPED_UNICODE part? In the past this has been the issue for myself. – Petay87 Jan 08 '19 at 10:08

0 Answers0