我的lumen版本,5.8

1.找到 app/Http/Controllers/Controller.php

修改成如下代码:

<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class Controller extends BaseController
{
    //重写认证错误消息
    protected function throwValidationException(Request $request, $validator)
    {

        $response = [
            'code' => 400,
            'msg'  => $validator->errors()->first(),
            'data' => []
        ];
        throw new ValidationException($validator,$this->buildFailedValidationResponse(
            $request, $response));

    }

//    返回成功消息
    protected function responseJson($data = [],$msg = 'success', $status = 200, array $headers = [], $options = 0){

        $response = [
            'code'  =>  $status,
            'msg'   =>  $msg,
            'data'  =>  $data
        ];
        return response()->json($response,$status, $headers, $options);
    }

}

2.在接口中使用

你的类要继承 App\Http\Controllers\Controller;


public function test(Request $request){
    //验证错误自动返回我们指定的格式
    $res = $this->validate($request,[
        'appid' =>  'required',
        'secret'=>  'required',
        'code'  =>  'required'
    ]);
    //成功消息,如果要反返回指定错误码的消息,$this->responseJson($res,'error',400);
    return $this->responseJson($res);
}

php使用Carbon获取2个日期之间的日期

<?php use Carbon\CarbonPeriod; $period = CarbonPeriod::create('2018-06-14', '2018-06-20'); // Iterate over the period fore...

阅读全文

php 解决超卖的几种方案(redis锁、mysql悲观锁)

1.创建商品和订单表模拟商品交易操作 fa_goods商品表,stock商品库存,test_order=订单表 2.添加一个测试商品数据(商品数量为150个) 测试并发购买的方法(gol...

阅读全文

centos8 yum安装php7.3

执行命令 $ yum install epel-release $ rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm $ dnf install -y https://rpms...

阅读全文

欢迎留言