project\config\errorcode.php <?php /** * Created by PhpStorm. * User: DELL * Date: 2018/9/6 * Time: 10:28 */ 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 => '成功', //单词书 310001 => '缺少必要的参数', 310002 => '单词书已经存在', 310003 => '软删除失败', 310004 => '单词书修改失败', 310005 => '软删除失败,存在单词列表', //单词列表 320001 => '缺少必要的参数', 320002 => '单词列已经存在', 320003 => '单词列排序变更失败', 320004 => '不存在的单词列', 320005 => '软删除失败,存在单词', 320006 => '软删除失败', ], ];
project\app\Http\Controllers\Controller.php
<?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, ]); } }