Laravel自定义 封装便捷返回Json数据格式引用
一般返回数据格式
return response()->json(['status' => 'success','code' => 200,'message' => '关注成功']); return response()->json(['status' => 'fail','code' => 500,'error' => '关注失败',]);
基类控制器
<?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 => '缺少必要的参数', //文章
503001 => '上传文件的格式不正确',
503002 => '同步成功-记录保存失败',
503003 => '权限错误',
503004 => '文章保存失败',
403017 => '临近定时时间不能取消发送任务',
403018 => '临近定时时间不能修改发送任务',
403019 => '超过发送时间不能发送',
403020 => '缺少发表记录ID参数',
//SMS
416001 => '添加成功,审核中,请耐心等待',
416002 => '签名添加失败',
], ];
可以对状态信息进行归类,如4--为用户端错误,5--位服务器端错误,2--为请求成功 。。。。。。。
返回引用
return $this->fail(503003); return $this->Success();
原文:https://blog.csdn.net/qq_34827048/article/details/78486599
Laravel自定义 封装便捷返回Json数据格式引用的更多相关文章
- MVC返回JSON数据格式书写方式
返回json数据格式,多个返回值加,隔开 [Route("api/users/web")] //如果不加这个路由请这样调用:/api/users/web?schoolname=十五 ...
- Mui.ajax请求服务器正确返回json数据格式
ajax: mui.ajax('http://server-name/login.php',{ data:{ username:'username', password:'password' }, d ...
- 转:spring mvc返回json数据格式
转:http://www.cnblogs.com/ssslinppp/p/4675495.html <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: ...
- 自定义统一api返回json格式(app后台框架搭建三)
在统一json自定义格式的方式有多种:1,直接重写@reposeBody的实现,2,自定义一个注解,自己去解析对象成为json字符串进行返回 第一种方式,我就不推荐,想弄得的话,可以自己去研究一下源码 ...
- Java 利用枚举封装接口返回 JSON 结构
利用枚举封装返回码和返回信息 package com.template.project.util; public enum StatusCode { SUCCESS(20000, "成功&q ...
- 关于ajax请求,返回json数据格式
使用servlet测试 后台数据为:返回类型没有设置(方式一) String str = "["+ "{ id:1, pId:0, name:\"可折腾的父节点 ...
- Webservice返回json数据格式
问题: 我将结果内容用字符串拼接成Json数据并返回的时候,会在结果前面添加xml头部,结果如下. <span ><string xmlns="http://tempuri ...
- WebApi关于配置全局返回Json数据格式时间以及命名小写
1.直接在Global文件中配置: 1 var formatters = GlobalConfiguration.Configuration.Formatters; 2 var jsonFormatt ...
- Juery返回Json数据格式,webForm中使用
此篇的详细篇 //webForm中使用异步就会用到一般处理程序,前台调用一般处理程序这个页面去执行里面的方法 using System.Web.Script.Serialization; Newton ...
随机推荐
- 关于使用spring mvc前后端上传数据日期格式
前端页面传入数据到后台,String,int等等类型都很友好,但是Date类型不能直接上传到服务器.否则会报异常的数量,设\(cnt1_x\)表示左端点为\(x\)的\(AA\)的数量,\(cnt2_x\)表示右端点为\(x\)的\(AA\)的数量,那么答案就是\(\sum ...
- React-使用Redux-thunk中间件实现ajax数据请求
把异步函数放在生命周期函数里写,生命周期函数会变得越来越复杂,组件会变得越来越大.Redux默认只处理同步,借助redux-thunk ,可以把异步请求放在actionCreators.js里管理,而 ...
- 读取Excel的记录并导入SQL数据库
准备一下,近段时间,需要把Excel的数据导入数据库中. 引用命名空间: using System.Configuration; using System.Data; using System.Dat ...
- (转)Syntax error:
但是运行时总是报下面这个错,如下: test11-loop.sh: 5: Syntax error: Bad for loop variable 几经查找语法,没有问题,后来在网上找到问题原因: 代码 ...