服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - Laravel 框架返回状态拦截代码

Laravel 框架返回状态拦截代码

2021-09-08 15:01妖怪都是妖怪 PHP教程

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

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

使用查询

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

拦截代码

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

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

原文链接:https://blog.csdn.net/qq_29755359/article/details/82107389

延伸 · 阅读

精彩推荐