一般返回?cái)?shù)據(jù)格式
return response()->json(['status' => 'success','code' => 200,'message' => '關(guān)注成功']);
return response()->json(['status' => 'fail','code' => 500,'error' => '關(guān)注失敗',]);
基類控制器
?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function success($data = [])
{
return response()->json([
'status' => true,
'code' => 200,
'message' => config('errorcode.code')[200],
'data' => $data,
]);
}
public function fail($code, $data = [])
{
return response()->json([
'status' => false,
'code' => $code,
'message' => config('errorcode.code')[(int) $code],
'data' => $data,
]);
}
}
errorcode文件
?php
return [
/*
|--------------------------------------------------------------------------
| customized http code
|--------------------------------------------------------------------------
|
| The first number is error type, the second and third number is
| product type, and it is a specific error code from fourth to
| sixth.But the success is different.
|
*/
'code' => [
200 => '成功',
200001 => '缺少必要的參數(shù)',
//文章
503001 => '上傳文件的格式不正確',
503002 => '同步成功-記錄保存失敗',
503003 => '權(quán)限錯(cuò)誤',
503004 => '文章保存失敗',
403017 => '臨近定時(shí)時(shí)間不能取消發(fā)送任務(wù)',
403018 => '臨近定時(shí)時(shí)間不能修改發(fā)送任務(wù)',
403019 => '超過(guò)發(fā)送時(shí)間不能發(fā)送',
403020 => '缺少發(fā)表記錄ID參數(shù)',
//SMS
416001 => '添加成功,審核中,請(qǐng)耐心等待',
416002 => '簽名添加失敗',
],
];
可以對(duì)狀態(tài)信息進(jìn)行歸類,如4--為用戶端錯(cuò)誤,5--位服務(wù)器端錯(cuò)誤,2--為請(qǐng)求成功 。。。。。。。
返回引用
return $this->fail(503003);
return $this->Success();
以上這篇Laravel自定義 封裝便捷返回Json數(shù)據(jù)格式的引用方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 淺談Java代碼的 微信長(zhǎng)鏈轉(zhuǎn)短鏈接口使用 post 請(qǐng)求封裝Json(實(shí)例)
- JS如何實(shí)現(xiàn)封裝列表右滑動(dòng)刪除收藏按鈕
- echarts.js 動(dòng)態(tài)生成多個(gè)圖表 使用vue封裝組件操作
- 原生JS封裝拖動(dòng)驗(yàn)證滑塊的實(shí)現(xiàn)代碼示例
- JavaScript面向?qū)ο笕齻€(gè)基本特征實(shí)例詳解【封裝、繼承與多態(tài)】
- Nodejs封裝類似express框架的路由實(shí)例詳解
- 如何基于js及java分析并封裝排序算法