婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Laravel 框架返回狀態攔截代碼

Laravel 框架返回狀態攔截代碼

熱門標簽:太原極信防封電銷卡 地圖標注入哪個科目 南寧crm外呼系統平臺 熱線電話機器人 事業單位如何百度地圖標注 福泉電話機器人 天津營銷電話機器人加盟代理 電銷招聘機器人 格陵蘭島地圖標注

可攔截系統的返回的狀態自己在單獨處理。

使用查詢

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 5.5 異常處理 & 錯誤日志的解決
  • Laravel手動返回錯誤碼示例

標簽:佳木斯 寶雞 自貢 香港 通化 阿克蘇 郴州 金華

巨人網絡通訊聲明:本文標題《Laravel 框架返回狀態攔截代碼》,本文關鍵詞  Laravel,框架,返回,狀態,攔截,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Laravel 框架返回狀態攔截代碼》相關的同類信息!
  • 本頁收集關于Laravel 框架返回狀態攔截代碼的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 琼结县| 本溪市| 台前县| 霍州市| 大田县| 玛曲县| 垦利县| 岳阳县| 罗甸县| 湘潭市| 北宁市| 阿坝| 汉川市| 方城县| 玉屏| 沙河市| 华坪县| 荥经县| 乌什县| 遂平县| 上饶县| 汝阳县| 青龙| 镇江市| 靖宇县| 安吉县| 延吉市| 阿拉善右旗| 龙门县| 东乡族自治县| 普洱| 惠州市| 小金县| 苏尼特右旗| 遂宁市| 阳城县| 巩义市| 泸定县| 宁津县| 安宁市| 江北区|