|
@@ -0,0 +1,30 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace App\Http\Controllers\Api;
|
|
4
|
+
|
|
5
|
+use Illuminate\Http\Response;
|
|
6
|
+use App\Http\Controllers\Controller;
|
|
7
|
+
|
|
8
|
+class ApiController extends Controller
|
|
9
|
+{
|
|
10
|
+ /**
|
|
11
|
+ * API 預設輸出格式
|
|
12
|
+ *
|
|
13
|
+ * @param array $data
|
|
14
|
+ * @param int $code
|
|
15
|
+ * @param string $message
|
|
16
|
+ * @return \Illuminate\Http\JsonResponse
|
|
17
|
+ */
|
|
18
|
+ protected function apiResponse(array $data = [], int $code = Response::HTTP_OK, string $message = '')
|
|
19
|
+ {
|
|
20
|
+ if (empty($message) && isset(Response::$statusTexts[$code])) {
|
|
21
|
+ $message = Response::$statusTexts[$code];
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ return response()->json([
|
|
25
|
+ 'code' => $code,
|
|
26
|
+ 'message' => $message,
|
|
27
|
+ 'data' => $data
|
|
28
|
+ ], $code);
|
|
29
|
+ }
|
|
30
|
+}
|