加入收藏 | 设为首页 | 会员中心 | 我要投稿 温州站长网 (https://www.0577zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Laravel下怎样实现返回状态拦截代码的操作?

发布时间:2021-12-05 00:53:28 所属栏目:PHP教程 来源:互联网
导读:本文给大家分享的时候Laravel下怎样实现返回状态拦截代码的操作,可拦截系统的返回的状态自己在单独处理,小编觉得比较实用,废话不多说,我们直接来看代码,需要的朋友可以参考。 使用查询 composer require betterde/response // 安装后直接调用以下 # st
本文给大家分享的时候Laravel下怎样实现返回状态拦截代码的操作,可拦截系统的返回的状态自己在单独处理,小编觉得比较实用,废话不多说,我们直接来看代码,需要的朋友可以参考。
 
使用查询
 
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 = []);
拦截代码
 
AppExceptionsHandler
<?php
 
namespace AppExceptions;
 
use Exception;
use IlluminateSupportFacadesLog;
use IlluminateDatabaseQueryException;
use AppTraitsResponseInterfaceResponse;
use IlluminateAuthAuthenticationException;
use IlluminateValidationValidationException;
use IlluminateAuthAccessAuthorizationException;
use IlluminateDatabaseEloquentModelNotFoundException;
use SymfonyComponentHttpKernelExceptionHttpException;
use IlluminateFoundationExceptionsHandler as ExceptionHandler;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
use SymfonyComponentHttpKernelExceptionUnauthorizedHttpException;
 
/**
 * 异常处理
 *
 * Date: 21/03/2018
 * @author George
 * @package AppExceptions
 */
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 IlluminateHttpRequest $request
 * @param Exception $exception
 * @return IlluminateContractsRoutingResponseFactory|SymfonyComponentHttpFoundationResponse
 */
 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 IlluminateHttpRequest $request
 * @param AuthenticationException $exception
 * @return IlluminateHttpJsonResponse|IlluminateHttpResponse
 */
public function unauthenticated($request, AuthenticationException $exception)
{
return failed('身份认证失败', 401);
 }
}

(编辑:温州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读