欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel 框架返回狀態(tài)攔截代碼

 更新時(shí)間:2019年10月18日 08:39:41   作者:妖怪都是妖怪  
今天小編就為大家分享一篇Laravel 框架返回狀態(tài)攔截代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

可攔截系統(tǒng)的返回的狀態(tài)自己在單獨(dú)處理。

使用查詢

composer require betterde/response
// 安裝后直接調(diào)用以下
# stored
return stored($data, $message = '創(chuàng)建成功');
 
#updated
return updated($data, $message = '更新成功');
 
#deleted
return deleted($message = '刪除成功');
 
#accepted
return accepted($message = '請求已接受,等待處理');
 
#notFound
return notFound($message = '您訪問的資源不存在');
 
#internalError
return internalError($message = '未知錯(cuò)誤導(dǎo)致請求失敗');
 
#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);
 }
 
	/**
	 * 攔截異常并生成對應(yīng)的響應(yīng)內(nèi)容
	 *
	 * 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)
 {
 	// 攔截?cái)?shù)據(jù)庫操作異常
// 	if ($exception instanceof QueryException) {
// 		Log::error($exception);
// 		return $this->internalError();
//		}
 
		// 攔截一般異常并生成響應(yīng)
		if ($exception instanceof GeneralException) {
			return failed($exception->getMessage(), $exception->getCode() ?: 500);
		}
 
		// 攔截404異常
		if ($exception instanceof ModelNotFoundException) {
			return $this->notFound();
		}
 
		// 攔截授權(quán)異常
		if ($exception instanceof AuthorizationException) {
			return failed('您無權(quán)訪問', 403);
		}
 
		// 參數(shù)驗(yàn)證錯(cuò)誤的異常,我們需要返回 400 的 http code 和一句錯(cuò)誤信息
		if ($exception instanceof ValidationException) {
			return failed(array_first(array_collapse($exception->errors())), 422);
		}
 
		// 用戶認(rèn)證的異常,我們需要返回 401 的 http code 和錯(cuò)誤信息
		if ($exception instanceof UnauthorizedHttpException) {
			return failed('未提供Token', 401);
		}
 
		// 捕獲404異常
		if ($exception instanceof NotFoundHttpException) {
 		return $this->notFound();
		}
 
  return parent::render($request, $exception);
 }
 
	/**
	 * 認(rèn)證失敗后拋出異常
	 *
	 * 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('身份認(rèn)證失敗', 401);
 }
}

以上這篇Laravel 框架返回狀態(tài)攔截代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Laravel 中獲取上一篇和下一篇數(shù)據(jù)

    Laravel 中獲取上一篇和下一篇數(shù)據(jù)

    這篇文章主要介紹了Laravel 中獲取上一篇和下一篇數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2015-07-07
  • smarty循環(huán)嵌套用法示例分析

    smarty循環(huán)嵌套用法示例分析

    這篇文章主要介紹了smarty循環(huán)嵌套用法,結(jié)合實(shí)例形式分析了Smarty模板嵌套循環(huán)的實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-07-07
  • PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記 (五) - PHP 命名空間

    PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記 (五) - PHP 命名空間

    PHP 在 5.3.0 以后的版本開始支持命名空間。什么是命名空間?從廣義上來說,命名空間是一種封裝事物的方法。在很多地方都可以見到這種抽象概念。
    2014-06-06
  • PHP實(shí)現(xiàn)圖片批量打包下載功能

    PHP實(shí)現(xiàn)圖片批量打包下載功能

    這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)圖片批量打包下載功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Laravel5.1 框架Middleware中間件基本用法實(shí)例分析

    Laravel5.1 框架Middleware中間件基本用法實(shí)例分析

    這篇文章主要介紹了Laravel5.1 框架Middleware中間件基本用法,結(jié)合實(shí)例形式分析了laravel5.1框架Middleware中間件功能、創(chuàng)建、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2020-01-01
  • Symfony2學(xué)習(xí)筆記之系統(tǒng)路由詳解

    Symfony2學(xué)習(xí)筆記之系統(tǒng)路由詳解

    這篇文章主要介紹了Symfony2系統(tǒng)路由,詳細(xì)分析了Symfony路由原理及路由的創(chuàng)建,配置與使用技巧,需要的朋友可以參考下
    2016-03-03
  • php微信公眾號js-sdk開發(fā)應(yīng)用

    php微信公眾號js-sdk開發(fā)應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了php微信公眾號js-sdk開發(fā)應(yīng)用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 如何讓搜索引擎抓取AJAX內(nèi)容解決方案

    如何讓搜索引擎抓取AJAX內(nèi)容解決方案

    談到AJAX很多人會聯(lián)想到JavaScript,直到現(xiàn)在為止各大搜索引擎對如:javascript、ajax、flash代碼生成的內(nèi)容都沒辦法很好的抓取。但很多站長都很喜歡這些效果,可是偏偏各大搜索引擎不能很好的抓取這些代碼所生成的內(nèi)容,使得很多站長都放棄了這些效果。
    2014-08-08
  • PHP實(shí)現(xiàn)加減乘除最簡單的實(shí)例分享

    PHP實(shí)現(xiàn)加減乘除最簡單的實(shí)例分享

    在本篇文章里小編給大家整理了一篇關(guān)于PHP實(shí)現(xiàn)簡單的加減乘除的實(shí)例內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。
    2021-08-08
  • thinkPHP簡單遍歷數(shù)組方法分析

    thinkPHP簡單遍歷數(shù)組方法分析

    這篇文章主要介紹了thinkPHP簡單遍歷數(shù)組方法,結(jié)合實(shí)例形式分析了thinkPHP使用volist標(biāo)簽遍歷數(shù)組的技巧,并對比分析了織夢cms的arclist標(biāo)簽加強(qiáng)對thinkPHP數(shù)組遍歷的理解,需要的朋友可以參考下
    2016-05-05

最新評論