laravel框架中引入全局错误码以及相关报错信息

17 篇文章 0 订阅
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,
        ]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值