当前位置:首页 > CMS教程 > 其它CMS > 列表

Laravel 框架返回状态拦截代码

发布:smiling 来源: PHP粉丝网  添加日期:2022-01-08 16:19:32 浏览: 评论:0 

今天小编就为大家分享一篇Laravel 框架返回状态拦截代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。

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

使用查询

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

拦截代码

App\Exceptions\Handler

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

Tags: Laravel框架返回状态拦截

分享到: