幽灵资源网 Design By www.bzswh.com
可拦截系统的返回的状态自己在单独处理。
使用查询
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 框架返回状态拦截代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
标签:
Laravel,框架,状态拦截
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。