laravel json utf 8

Solutions on MaxInterview for laravel json utf 8 by the best coders in the world

showing results for - "laravel json utf 8"
Juan Manuel
06 Apr 2017
1//Create this middleware below:
2
3class JsonUtf8Middleware
4{
5    /**
6     * Handle an incoming request.
7     *
8     * @param  \Illuminate\Http\Request  $request
9     * @param  \Closure  $next
10     * @return mixed
11     */
12    public function handle(Request $request, Closure $next)
13    {
14        $data =  $next($request);
15        if($data instanceof JsonResponse) {
16            $data->withHeaders(['Content-Type' => "application/json; charset=utf-8"]);
17            $data->setEncodingOptions(JSON_UNESCAPED_UNICODE);
18        }
19
20        return $data;
21    }
22}
23/**
24 * It will set content-type application/json with charset UTF-8 (line 16). 
25 * If you want UTF-8 serie, you want to apply UNICODE.
26 * By default, the responde will scape UNICODE, 
27 * replacing accented words for example.
28 * So, that why this middleware will unscape unicode at (line 17)
29 * Don't forget to configure it App\Http\Kernel.php!
30*/