Laravel下怎样实现返回状态拦截代码的操作?
Admin 2021-10-22 群英技术资讯 893 次浏览
本文给大家分享的时候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 = []);
拦截代码
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返回状态拦截代码的操作,上述代码具有一定的借鉴价值,有需要的朋友可以参考。最后想要了解更多可以继续浏览群英网络其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Laravel Gates(拦截器)允许你授权用户访问应用程序的某些区域。你可以轻松地在应用程序中定义拦截器,然后使用它们来允许或拒绝访问。
用PHP怎样实现堆排序?要解决这个问题,首先我们需要了解关于堆,完成二叉树以及堆排序的原理,本文后面有堆排序的PHP实现代码,有需要的朋友可以参考一下。
很多朋友想要知道PHP怎么样快速导出百万级数据到CSV或者EXCEL文件中,因此,这篇文章就给大家分享一下php百万级数据导出的操作,下面是导出思路,导出源码以及测试,有需要的朋友可以看一下,希望大家有收获。
本篇文章给大家带来了关于laravel的相关知识,其中主要介绍了迁移文件的相关问题,迁移文件其实就是相对数据库的一个版本控制,让我们去能够轻松的定义和共享程序里面的一些数据结构,下面一起来看一下,希望对大家有帮助。
这篇文章主要介绍了Laravel框架Blade模板简介及模板继承用法,结合实例形式分析了Laravel框架Blade模板功能及模板继承相关操作技巧,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008