经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
Laravel 框架返回状态拦截代码
来源:jb51  时间:2019/10/18 11:29:55  对本文有异议

可拦截系统的返回的状态自己在单独处理。

使用查询

  1. composer require betterde/response
  2. // 安装后直接调用以下
  3. # stored
  4. return stored($data, $message = '创建成功');
  5. #updated
  6. return updated($data, $message = '更新成功');
  7. #deleted
  8. return deleted($message = '删除成功');
  9. #accepted
  10. return accepted($message = '请求已接受,等待处理');
  11. #notFound
  12. return notFound($message = '您访问的资源不存在');
  13. #internalError
  14. return internalError($message = '未知错误导致请求失败');
  15. #failed
  16. return failed($message, $code = Response::HTTP_BAD_REQUEST);
  17. #success
  18. return success($data);
  19. #message
  20. return message($message, $code = Response::HTTP_OK);
  21. #respond
  22. return respond($data = [], $message = '请求成功', array $header = []);

拦截代码

  1. App\Exceptions\Handler
  1. <?php
  2. namespace App\Exceptions;
  3. use Exception;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Database\QueryException;
  6. use App\Traits\Response\InterfaceResponse;
  7. use Illuminate\Auth\AuthenticationException;
  8. use Illuminate\Validation\ValidationException;
  9. use Illuminate\Auth\Access\AuthorizationException;
  10. use Illuminate\Database\Eloquent\ModelNotFoundException;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  15. /**
  16. * 异常处理
  17. *
  18. * Date: 21/03/2018
  19. * @author George
  20. * @package App\Exceptions
  21. */
  22. class Handler extends ExceptionHandler
  23. {
  24. use InterfaceResponse;
  25. /**
  26. * 定义不需要记录的异常类
  27. *
  28. * @var array
  29. */
  30. protected $dontReport = [
  31. HttpException::class,
  32. ValidationException::class,
  33. ModelNotFoundException::class,
  34. AuthorizationException::class,
  35. AuthenticationException::class,
  36. ];
  37. /**
  38. * A list of the inputs that are never flashed for validation exceptions.
  39. *
  40. * @var array
  41. */
  42. protected $dontFlash = [
  43. 'password',
  44. 'password_confirmation',
  45. ];
  46. /**
  47. * 定义需要记录的异常
  48. *
  49. * Date: 21/03/2018
  50. * @author George
  51. * @param Exception $exception
  52. * @return mixed|void
  53. * @throws Exception
  54. */
  55. public function report(Exception $exception)
  56. {
  57. parent::report($exception);
  58. }
  59. /**
  60. * 拦截异常并生成对应的响应内容
  61. *
  62. * Date: 21/03/2018
  63. * @author George
  64. * @param \Illuminate\Http\Request $request
  65. * @param Exception $exception
  66. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  67. */
  68. public function render($request, Exception $exception)
  69. {
  70. // 拦截数据库操作异常
  71. // if ($exception instanceof QueryException) {
  72. // Log::error($exception);
  73. // return $this->internalError();
  74. // }
  75. // 拦截一般异常并生成响应
  76. if ($exception instanceof GeneralException) {
  77. return failed($exception->getMessage(), $exception->getCode() ?: 500);
  78. }
  79. // 拦截404异常
  80. if ($exception instanceof ModelNotFoundException) {
  81. return $this->notFound();
  82. }
  83. // 拦截授权异常
  84. if ($exception instanceof AuthorizationException) {
  85. return failed('您无权访问', 403);
  86. }
  87. // 参数验证错误的异常,我们需要返回 400 的 http code 和一句错误信息
  88. if ($exception instanceof ValidationException) {
  89. return failed(array_first(array_collapse($exception->errors())), 422);
  90. }
  91. // 用户认证的异常,我们需要返回 401 的 http code 和错误信息
  92. if ($exception instanceof UnauthorizedHttpException) {
  93. return failed('未提供Token', 401);
  94. }
  95. // 捕获404异常
  96. if ($exception instanceof NotFoundHttpException) {
  97. return $this->notFound();
  98. }
  99. return parent::render($request, $exception);
  100. }
  101. /**
  102. * 认证失败后抛出异常
  103. *
  104. * Date: 2018/5/27
  105. * @author George
  106. * @param \Illuminate\Http\Request $request
  107. * @param AuthenticationException $exception
  108. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
  109. */
  110. public function unauthenticated($request, AuthenticationException $exception)
  111. {
  112. return failed('身份认证失败', 401);
  113. }
  114. }

以上这篇Laravel 框架返回状态拦截代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号