我的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);
}

Mysql 分页优化

在系统中需要进行分页的时候,我们通常会使用limit 加上偏移量的方法实现,再加上合适的order by 子句,如果有对应的索引,通常效率会很不错,否则mysql需要...

阅读全文

lcobucci/jwt —— 一个轻松生成jwt token的插件

github: https://github.com/lcobucci/jwt/tree/3.2 1.安装 PHP 5.5+ (v3.2) and PHP 7.1 (v4.x) OpenSSL Extension composer require lcobucci/jwt 2. ...

阅读全文

laravel/lumen —— API 插件 dingo/api

文档:https://github.com/dingo/api/wiki/Installation 1.安装 #PHP ^7.1 (However soon to be PHP 7.2) composer require dingo/api #LARAVEL php artisa...

阅读全文

欢迎留言