Laravel 框架返回狀態(tài)攔截代碼
可攔截系統(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 = '請(qǐng)求已接受,等待處理'); #notFound return notFound($message = '您訪問(wèn)的資源不存在'); #internalError return internalError($message = '未知錯(cuò)誤導(dǎo)致請(qǐng)求失敗'); #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 = '請(qǐng)求成功', 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);
}
/**
* 攔截異常并生成對(duì)應(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ù)庫(kù)操作異常
// 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('您無(wú)權(quán)訪問(wè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)攔截代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
php循環(huán)table實(shí)現(xiàn)一行兩列顯示的方法
這篇文章主要介紹了php循環(huán)table實(shí)現(xiàn)一行兩列顯示的方法,本文直接給出實(shí)現(xiàn)代碼,重點(diǎn)就是在取余方法的運(yùn)用,需要的朋友可以參考下2015-06-06
PHP實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇PHP實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給的大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
php設(shè)計(jì)模式之觀察者模式實(shí)例詳解【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之觀察者模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了php觀察者模式相關(guān)概念、原理、用法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
PHP將url生成二維碼并實(shí)現(xiàn)掃碼跳轉(zhuǎn)示例詳解
這篇文章主要為大家介紹了PHP將url生成二維碼并實(shí)現(xiàn)掃碼跳轉(zhuǎn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
基于linnux+phantomjs實(shí)現(xiàn)生成圖片格式的網(wǎng)頁(yè)快照
在代碼區(qū)看到一個(gè)生成站點(diǎn)快照的代碼,看了半天才發(fā)現(xiàn),作者僅僅貼出來(lái)業(yè)務(wù)代碼,最核心的生成快照?qǐng)D片的代碼反而沒(méi)有給出來(lái)。 以前記得google搜索提供站點(diǎn)縮略圖,那時(shí)候覺(jué)得好神奇,但是沒(méi)有花時(shí)間去做深入的調(diào)研。昨天又遇到了,那就順便調(diào)研下吧。2015-04-04
一個(gè)用php實(shí)現(xiàn)的獲取URL信息的類
一個(gè)用php實(shí)現(xiàn)的獲取URL信息的類...2007-01-01
laravel 解決ajax異步提交數(shù)據(jù),并還回填充表格的問(wèn)題
今天小編就為大家分享一篇laravel 解決ajax異步提交數(shù)據(jù),并還回填充表格的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

