response.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * This file is part of the jiannei/laravel-response.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. return [
  11. /*
  12. |--------------------------------------------------------------------------
  13. | Set the http status code when the response fails
  14. |--------------------------------------------------------------------------
  15. |
  16. | the reference options are false, 200, 500
  17. |
  18. | false, stricter http status codes such as 404, 401, 403, 500, etc. will be returned
  19. | 200, All failed responses will also return a 200 status code
  20. | 500, All failed responses return a 500 status code
  21. */
  22. 'error_code' => false,
  23. // You can use enumerations to define the code when the response is returned,
  24. // and set the response message according to the locale
  25. //
  26. // The following two enumeration packages are good choices
  27. //
  28. // https://github.com/Jiannei/laravel-enum
  29. // https://github.com/BenSampo/laravel-enum
  30. 'enum' => '', // \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::class
  31. // You can set some attributes (eg:code/message/header/options) for the exception, and it will override the default attributes of the exception
  32. 'exception' => [
  33. \Illuminate\Validation\ValidationException::class => [
  34. 'code' => 422,
  35. ],
  36. \Illuminate\Auth\AuthenticationException::class => [
  37. ],
  38. \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => [
  39. 'message' => '',
  40. ],
  41. \Illuminate\Database\Eloquent\ModelNotFoundException::class => [
  42. 'message' => '',
  43. ],
  44. ],
  45. // Set the structure of the response data
  46. 'format' => [
  47. 'fields' => [
  48. 'status' => ['alias' => 'status', 'show' => true],
  49. 'code' => ['alias' => 'code', 'show' => true],
  50. 'message' => ['alias' => 'message', 'show' => true],
  51. 'error' => ['alias' => 'error', 'show' => true],
  52. 'data' => [
  53. 'alias' => 'data',
  54. 'show' => true,
  55. 'fields' => [
  56. // When data is nested with data, such as returning paged data, you can also set an alias for the inner data
  57. 'data' => ['alias' => 'data', 'show' => true], // data/rows/list
  58. 'meta' => [
  59. 'alia' => 'meta',
  60. 'show' => true,
  61. 'fields' => [
  62. 'pagination' => [
  63. 'alias' => 'pagination',
  64. 'show' => true,
  65. 'fields' => [
  66. 'total' => ['alias' => 'total', 'show' => true],
  67. 'count' => ['alias' => 'count', 'show' => true],
  68. 'per_page' => ['alias' => 'per_page', 'show' => true],
  69. 'current_page' => ['alias' => 'current_page', 'show' => true],
  70. 'total_pages' => ['alias' => 'total_pages', 'show' => true],
  71. 'links' => [
  72. 'alias' => 'links',
  73. 'show' => true,
  74. 'fields' => [
  75. 'previous' => ['alias' => 'previous', 'show' => true],
  76. 'next' => ['alias' => 'next', 'show' => true],
  77. ],
  78. ],
  79. ],
  80. ],
  81. ],
  82. ],
  83. ],
  84. ],
  85. ],
  86. ],
  87. ];