DDR爱好者之家 Design By 杰米
可拦截系统的返回的状态自己在单独处理。
使用查询
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 = []);
拦截代码
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\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 框架返回状态拦截代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2025年03月17日
2025年03月17日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]