对API响应做统一的封装,让代码看起来更清晰一些。
HttpService 主要提供3个方法:success, error, validate
success和error方法统一响应相同格式的数据结构:
{“msg”: “测试错误”,”data”: [],”code”: 0}
validate方法只是基于系统Validator的封装。
代码:
namespace App\Service; | |
use Illuminate\Support\Facades\Validator; | |
class HttpService | |
{ | |
public function __construct() | |
{ | |
} | |
/** | |
* success | |
* @param $message | |
* @param array $content | |
* @param int $code | |
* @param int $status | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public static function success(string $message, array $content = [], int $code = 1, int $status = 200) | |
{ | |
$data['msg'] = $message; | |
$data['data'] = $content; | |
$data['code'] = $code; | |
return response()->json($data, $status); | |
} | |
/** | |
* error | |
* @param $message | |
* @param array $content | |
* @param int $code | |
* @param int $status | |
* @return \Illuminate\Http\JsonResponse | |
*/ | |
public static function error(string $message, array $content = [], int $code = 0, int $status = 200) | |
{ | |
$data['msg'] = $message; | |
$data['data'] = $content; | |
$data['code'] = $code; | |
return response()->json($data, $status); | |
} | |
/** | |
* 字段校验 | |
* @param $all | |
* @param $rule | |
* @param array $message | |
* @param array $attribute | |
* @return bool|string | |
*/ | |
public static function validate(array $all, array $rule, array $message = [], array $attribute = []) | |
{ | |
$message = $message ?: self::getMessage(); | |
$attribute = $attribute ?: self::getAttributes(); | |
$validate = Validator::make($all, $rule, $message, $attribute); | |
if ($validate->fails()) { | |
return implode(' ', $validate->errors()->all()); | |
} | |
return false; | |
} | |
/** | |
* 字段校验规则转义(爱写不写) | |
* 默认: resource/lang/en/validation.php | |
* @return array | |
*/ | |
public static function getMessage() | |
{ | |
return [ | |
'required' => '字段 :attribute 是必须的.', | |
'unique' => '字段 :attribute 数据已经存在.', | |
'between' => '字段 :attribute 必须在 :min - :max.之间', | |
]; | |
} | |
/** | |
* 字段校验字段转义(爱写不写) | |
* 默认: resource/lang/en/validation.php (attributes) | |
* @return array | |
*/ | |
public static function getAttributes() | |
{ | |
return []; | |
} | |
} |
然后来简单对比一下:
官方手册demo写法:
public function store(Request $request) | |
{ | |
$validator = Validator::make($request->all(), [ | |
'title' => 'required|unique:posts|max:255', | |
'body' => 'required', | |
]); | |
if ($validator->fails()) { | |
return $validator->errors(); | |
} | |
// response json | |
return response()->json(); | |
} |
基于HttpService类:
public function storeNew(Request $request) | |
{ | |
if ($error = HttpService::validate($request->all(), [ | |
'title' => 'required|unique:posts|max:255', | |
'body' => 'required', | |
])) { | |
return Http::error($error); | |
} | |
return HttpService::success('ok'); | |
} |
个人觉得好像更简单优雅了一些,实际也没啥区别。
小结:非喜勿扰,全凭喜好
附:laravel 本地语言包:
composer require caouecs/laravel-lang
1.复制相应的语言包文件(/verdor/caouecs/laravel-lang)到 /resource/lang
2.修改配置文件/config/app.php。
//修改为你使用的语言包 | |
'locale' => 'zh_CN', |
其他:一个好玩的composer拼音译文包: