Laravel 框架返回状态拦截代码
网络编程 2021-07-04 23:05www.168986.cn编程入门
今天长沙网络推广就为大家分享一篇Laravel 框架返回状态拦截代码,具有很好的参考价值,希望对大家有所帮助。一起跟随长沙网络推广过来看看吧
可拦截系统的返回的状态自己在单独处理。
使用查询
poser require betterde/response // 安装后直接调用以下 # stored return stored($data, $message = '创建成功'); #updated return updated($data, $message = '更新成功'); #deleted return deleted($message = '删除成功'); #aepted return aepted($message = '请求已接受,等待处理'); #notFound return notFound($message = '您访问的资源不存在'); #internalError return internalError($message = '未知错误导致请求失败'); #failed return failed($message, $code = Response::HTTP_BAD_REQUEST); #suess return suess($data); #message return message($message, $code = Response::HTTP_OK); #respond return respond($data = [], $message = '请求成功', array $header = []);
拦截代码
App\Exceptions\Handler
<?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\Aess\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 Gee @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 Gee @param Exception $exception @return mixed|void @throws Exception / public function report(Exception $exception) { parent::report($exception); } / 拦截异常并生成对应的响应内容 Date: 21/03/2018 @author Gee @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 Gee @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 框架返回状态拦截代码就是长沙网络推广分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持狼蚁SEO。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指