JSONレスポンスのUnicodeエスケープの無効化について一元化する

JSONレスポンスのUnicodeエスケープの無効化について一元化する

JSONレスポンスのUnicodeエスケープの無効化について

参考記事: https://nextat.co.jp/staff/archives/203

ユニコード無効化用のミドルウェアを作ればいちいち「JSON_UNESCAPED_UNICODE」を書いて回らなくても一括制御できるようです。

(丸々参考にさせて頂きました。)

app\Http\Middleware\UnescapeJsonResponse.php

class UnescapeJsonResponse
{
    /**
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function handle($request, \Closure $next)
    {
        $response = $next($request);

        // JSON以外はそのまま
        if (!$response instanceof JsonResponse) {
            return $response;
        }

        // エンコードオプションを追加して設定し直す
        $newEncodingOptions = $response->getEncodingOptions() | JSON_UNESCAPED_UNICODE;
        $response->setEncodingOptions($newEncodingOptions);

        return $response;
    }
}

app\Http\Kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\UnescapeJsonResponse::class,
    ];

後は適当なところでresponce()->jsonを使うと

            return response()->json([
                'result' => $data
            ]);

Before

{"result":[{"id":"86bNh_wjpVQ","title":"\u3010\u5fc3\u970a\u3011\u3053\u306e\u30c8\u30f3\u30cd\u30eb\u30e4\u30d0\u30a4\u3002\u5b8c\u5168\u306b\u805e\u3053\u3048\u307e\u3057\u305f\u3002","thumbnail":"https:\/\/i.ytimg.com\/vi\/86bNh_wjpVQ\/default.jpg","description":"\u30c1\u30e3\u30f3\u30cd\u30eb\u767b\u9332\u3088\u308d\u3057\u304f\u304a\u306d\u304c\u3044\u3057\u307e\u3059 !\nMy name is Hajime! \n\n\u30d5\u30a1\u30f3\u30b5\u30a4\u30c8\u304c\u51fa\u6765\u307e\u3057\u305f\uff01\uff01\uff01\n\u25bd\u300c\u306f\u3058\u30e1\u30fc\u30ce\u300d\uff1ahttps:\/\/goo.gl\/8XTb5t\n\n\u306f\u3058\u3081\u3057\u3083\u3061\u3087\u30fc\u306e\u7551\uff1ahttps:\/\/www.youtube.com\/channel\/UClKeJXipXwX7_ZGxOBnMQyw\n\n\u306f\u3058\u3081\u3057\u3083\u3061\u3087\u30fc2\uff08\u30b2\u30fc\u30e0\u5b9f\u6cc1\u306a\u3069\uff09\uff1ahttps:\/\/www.youtube.com\/user\/hajimexgame\n\ntwitter: https:\/\/twitter.com\/hajimesyacho\n\nInstagram: https:\/\/www.instagram.com\/hajimesyachodesu\/?hl=en\n\n\u904a\u622f\u738b\u7528Twitter:https:\/\/twitter.com\/duelhajime\n\n755:http:\/\/7gogo.jp\/lp\/ztf2d8Wmk2AWkVIvojdMdG==\n\nLINE\u30b9\u30bf\u30f3\u30d7\u306f\u3053\u3061\u3089\uff01\n\u306f\u3058\u3081\u3057\u3083\u3061\u3087\u30fc\nhttps:\/\/store.line.me\/stickershop\/product\/1072663\/ja\n\n\u306f\u3058\u3081\u3057\u3083\u3061\u3087\u30fc2\nhttps:\/\/store.line.me\/stickershop\/product\/1169745\/ja\n\n\u30fb\u52d5\u753b\u5185\u306b\u304a\u3051\u308b\u7d20\u6750\u63d0\u4f9b \n\uff3f\u4eba\u4eba\u4eba\u4eba\uff3f\n\uff1e\u3000PIXTA\u3000\uff1c\n\uffe3Y^Y^Y\uffe3\n\n------------------------------------------------------------------------------ \n\u697d\u66f2\u63d0\u4f9b\uff1aProduction Music by http:\/\/www.epidemicsound.com \n                    \u30d5\u30ea\u30fcBGM DOVA-SYNDROME by http:\/\/dova-s.jp\/\n------------------------------------------------------------------------------","channel_id":"UCgMPP6RRjktV7krOfyUewqw","category_id":24,"published_at":"2019-10-29T12:16:35.000Z","tags":"[\"hajime\",\"\u306f\u3058\u3081\u3057\u3083\u3061\u3087\u30fc\",\"funny\",\"crazy\",\"japanese\",\"\u306f\u3058\u3081\"]","like_count":16451,"bad_count":406,"view_count":449691,"favorite_count":0,"comment_count":3677,"popular_order":1,"is_deleted":0,"created_at":"2019-10-30 08:06:25","updated_at":"2019-10-30 08:06:25"},{"id":"IC9urbiVp4M","title":"Introducing AirPods Pro \u2014 Apple","thumbnail":"https:\/\/i.ytimg.com\/vi\/IC9urbiVp4M\/default.jpg","description":"AirPods Pro. Active Noise Cancellation for immersive sound. Transparency mode to hear what\u2019s happening around you. And three sizes of silicone tips for a customizable fit. It\u2019s magic like you\u2019ve never heard. Order online now. Available in stores October 30.\n\nLearn more about AirPods Pro at https:\/\/apple.co\/2BMFxbf\n\nSong: \u201cLenguas\u201d by Elephant Heart http:\/\/apple.co\/ElephantHeart","channel_id":"UCE_M8A5yxnLfW0KghEeajjw","category_id":28,"published_at":"2019-10-28T16:30:01.000Z","tags":"[\"Apple\",\"AirPods Pro\",\"AirPods\",\"Apple AirPods Pro\",\"New AirPods\",\"Apple AirPods\",\"Apple in-ear headphones\",\"New Apple Headphones\",\"AirPod Pro\",\"Latest Apple headphones\",\"Active Noise Cancelation\",\"Transparency mode\",\"Custom Fit\",\"Adaptive EQ\",\"Noise Cancelation\",\"Bluetooth headphones\",\"Wireless case\",\"Elephant Heart\",\"Lenguas\",\"Apple AirPods Ad\",\"New AirPods video\",\"Apple Announcement\",\"New Apple Product\",\"Mac\",\"iPhone\",\"Apple Watch\",\"iPad\",\"Apple TV\",\"Earbuds\"]","like_count":255921,"bad_count":53373,"view_count":7406753,"favorite_count":0,"comment_count":0,"popular_order":1,"is_deleted":0,"created_at":"2019-10-30 08:06:25","updated_at":"2019-10-30 08:06:25"}]}

After

{"result":[{"id":"86bNh_wjpVQ","title":"【心霊】このトンネルヤバイ。完全に聞こえました。","thumbnail":"https:\/\/i.ytimg.com\/vi\/86bNh_wjpVQ\/default.jpg","description":"チャンネル登録よろしくおねがいします !\nMy name is Hajime! \n\nファンサイトが出来ました!!!\n▽「はじメーノ」:https:\/\/goo.gl\/8XTb5t\n\nはじめしゃちょーの畑:https:\/\/www.youtube.com\/channel\/UClKeJXipXwX7_ZGxOBnMQyw\n\nはじめしゃちょー2(ゲーム実況など):https:\/\/www.youtube.com\/user\/hajimexgame\n\ntwitter: https:\/\/twitter.com\/hajimesyacho\n\nInstagram: https:\/\/www.instagram.com\/hajimesyachodesu\/?hl=en\n\n遊戯王用Twitter:https:\/\/twitter.com\/duelhajime\n\n755:http:\/\/7gogo.jp\/lp\/ztf2d8Wmk2AWkVIvojdMdG==\n\nLINEスタンプはこちら!\nはじめしゃちょー\nhttps:\/\/store.line.me\/stickershop\/product\/1072663\/ja\n\nはじめしゃちょー2\nhttps:\/\/store.line.me\/stickershop\/product\/1169745\/ja\n\n・動画内における素材提供 \n_人人人人_\n> PIXTA <\n ̄Y^Y^Y ̄\n\n------------------------------------------------------------------------------ \n楽曲提供:Production Music by http:\/\/www.epidemicsound.com \n                    フリーBGM DOVA-SYNDROME by http:\/\/dova-s.jp\/\n------------------------------------------------------------------------------","channel_id":"UCgMPP6RRjktV7krOfyUewqw","category_id":24,"published_at":"2019-10-29T12:16:35.000Z","tags":"[\"hajime\",\"はじめしゃちょー\",\"funny\",\"crazy\",\"japanese\",\"はじめ\"]","like_count":16451,"bad_count":406,"view_count":449691,"favorite_count":0,"comment_count":3677,"popular_order":1,"is_deleted":0,"created_at":"2019-10-30 08:06:25","updated_at":"2019-10-30 08:06:25"},{"id":"IC9urbiVp4M","title":"Introducing AirPods Pro — Apple","thumbnail":"https:\/\/i.ytimg.com\/vi\/IC9urbiVp4M\/default.jpg","description":"AirPods Pro. Active Noise Cancellation for immersive sound. Transparency mode to hear what’s happening around you. And three sizes of silicone tips for a customizable fit. It’s magic like you’ve never heard. Order online now. Available in stores October 30.\n\nLearn more about AirPods Pro at https:\/\/apple.co\/2BMFxbf\n\nSong: “Lenguas” by Elephant Heart http:\/\/apple.co\/ElephantHeart","channel_id":"UCE_M8A5yxnLfW0KghEeajjw","category_id":28,"published_at":"2019-10-28T16:30:01.000Z","tags":"[\"Apple\",\"AirPods Pro\",\"AirPods\",\"Apple AirPods Pro\",\"New AirPods\",\"Apple AirPods\",\"Apple in-ear headphones\",\"New Apple Headphones\",\"AirPod Pro\",\"Latest Apple headphones\",\"Active Noise Cancelation\",\"Transparency mode\",\"Custom Fit\",\"Adaptive EQ\",\"Noise Cancelation\",\"Bluetooth headphones\",\"Wireless case\",\"Elephant Heart\",\"Lenguas\",\"Apple AirPods Ad\",\"New AirPods video\",\"Apple Announcement\",\"New Apple Product\",\"Mac\",\"iPhone\",\"Apple Watch\",\"iPad\",\"Apple TV\",\"Earbuds\"]","like_count":255921,"bad_count":53373,"view_count":7406753,"favorite_count":0,"comment_count":0,"popular_order":1,"is_deleted":0,"created_at":"2019-10-30 08:06:25","updated_at":"2019-10-30 08:06:25"}]}

JSON_UNESCAPED_UNICODE を書いて回る必要がなくなった。。

助かる。。