Laravel自定义 封装便捷返回Json数据格式引用
来源:互联网 发布:linux下重启mysql 编辑:程序博客网 时间:2024/06/18 18:50
- 一般返回数据格式
return response()->json(['status' => 'success','code' => 200,'message' => '关注成功']); return response()->json(['status' => 'fail','code' => 500,'error' => '关注失败',]);
- 基类控制器
<?phpnamespace 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文件
<?phpreturn [ /* |-------------------------------------------------------------------------- | 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();
阅读全文
0 0
- Laravel自定义 封装便捷返回Json数据格式引用
- thinkphp返回Json数据格式
- SpingMVC返回json数据格式
- PHP 封装ajax返回数据格式
- thinkphp返回Json的数据格式
- spring mvc 返回json数据格式
- RESTEasy之返回JSON数据格式
- SpringMVC配置返回JSON数据格式
- andriod json数据格式的读取封装实例
- 将数据封装为Json数据格式
- json数据格式常用解析和封装方式
- 将数据封装为Json数据格式
- 将数据封装为Json数据格式
- 将数据封装为Json数据格式
- 关于返回json数据格式的问题
- JAVA 操作树返回JSON数据格式
- struts2、servlet中返回json数据格式
- ajax请求数据,返回json数据格式
- ip端口站群的优缺点
- 使用tensorflow深度学习识别验证码
- 国外大型网站使用到编程语言
- 欢迎使用CSDN-markdown编辑器
- PHP 常见错误码
- Laravel自定义 封装便捷返回Json数据格式引用
- 观察者模式及应用场景
- C/C++ 第五周线性表 项目(一)建立顺序栈的算法库
- VS2010 MFC中tab control使用方法
- 20.Struts2_主题
- java构造函数
- Pandas:将两个相同结构的DataFrame的数据合并得到一个新的Dataframe
- 第十周 【项目2
- NOIP2001 统计单词个数 rabin-karp哈希