diff --git a/app/Contracts/Resource.php b/app/Contracts/Resource.php index 069808a2..f31f9e32 100644 --- a/app/Contracts/Resource.php +++ b/app/Contracts/Resource.php @@ -2,7 +2,10 @@ namespace App\Contracts; +use App\Support\Resources\CustomAnonymousResourceCollection; +use App\Support\Resources\CustomPaginatedResourceResponse; use Illuminate\Http\Resources\Json\JsonResource; +use Illuminate\Pagination\AbstractPaginator; /** * Base class for a resource/response @@ -26,4 +29,28 @@ class Resource extends JsonResource } } } + + /** + * Customize the response to exclude all the extra data that isn't used. Based on: + * https://gist.github.com/derekphilipau/4be52164a69ce487dcd0673656d280da + * + * @param \Illuminate\Http\Request $request + * + * @return \Illuminate\Http\JsonResponse + */ + public function toResponse($request) + { + return $this->resource instanceof AbstractPaginator + ? (new CustomPaginatedResourceResponse($this))->toResponse($request) + : parent::toResponse($request); + } + + public static function collection($resource) + { + return tap(new CustomAnonymousResourceCollection($resource, static::class), function ($collection) { + if (property_exists(static::class, 'preserveKeys')) { + $collection->preserveKeys = (new static([]))->preserveKeys === true; + } + }); + } } diff --git a/app/Http/Middleware/ApiAuth.php b/app/Http/Middleware/ApiAuth.php index cb889812..ea449626 100644 --- a/app/Http/Middleware/ApiAuth.php +++ b/app/Http/Middleware/ApiAuth.php @@ -50,6 +50,9 @@ class ApiAuth implements Middleware return $user; }); + // Force english locale for API + app()->setLocale('en'); + return $next($request); } diff --git a/app/Http/Middleware/JsonResponse.php b/app/Http/Middleware/JsonResponse.php index 53b3f17d..7a848e77 100644 --- a/app/Http/Middleware/JsonResponse.php +++ b/app/Http/Middleware/JsonResponse.php @@ -15,6 +15,7 @@ class JsonResponse implements Middleware { $response = $next($request); $response->headers->set('Content-Type', 'application/json'); + $response->headers->set('charset', 'utf-8'); return $response; } diff --git a/app/Support/Resources/CustomAnonymousResourceCollection.php b/app/Support/Resources/CustomAnonymousResourceCollection.php new file mode 100644 index 00000000..5397e2bc --- /dev/null +++ b/app/Support/Resources/CustomAnonymousResourceCollection.php @@ -0,0 +1,16 @@ +resource instanceof AbstractPaginator + ? (new CustomPaginatedResourceResponse($this))->toResponse($request) + : parent::toResponse($request); + } +} diff --git a/app/Support/Resources/CustomPaginatedResourceResponse.php b/app/Support/Resources/CustomPaginatedResourceResponse.php new file mode 100644 index 00000000..7ddcbdf7 --- /dev/null +++ b/app/Support/Resources/CustomPaginatedResourceResponse.php @@ -0,0 +1,26 @@ +assertJson(['data' => ['name' => $airline->name]]); } + public function testGetAirlinesChineseChars() + { + $this->user = factory(User::class)->create([ + 'airline_id' => 0, + ]); + + factory(Airline::class)->create([ + 'icao' => 'DKH', + 'name' => '吉祥航空', + ]); + + factory(Airline::class)->create([ + 'icao' => 'CSZ', + 'name' => '深圳航空', + ]); + + factory(Airline::class)->create([ + 'icao' => 'CCA', + 'name' => '中国国际航空', + ]); + + factory(Airline::class)->create([ + 'icao' => 'CXA', + 'name' => '厦门航空', + ]); + + $res = $this->get('/api/airlines'); + $this->assertTrue($res->isOk()); + } + /** * @throws Exception */