响应宏
laravel 中的响应宏,说明文档中有,lumen的没有找到。于是参考laravel 项目中的响应宏写了个Lumen的
1. 新建文件 App\Providers\ResponseMacroServiceProvider.php
<?php
namespace App\Providers;
use Laravel\Lumen\Http\ResponseFactory;
use Illuminate\Support\ServiceProvider;
class ResponseMacroServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
ResponseFactory::macro('success', function ($data) {
return ResponseFactory::json([
'errors' => false,
'data' => $data,
]);
});
ResponseFactory::macro('error', function ($message, $status = 400) {
return ResponseFactory::json([
'message' => $status.' error',
'errors' => [
'message' => [$message],
],
'status_code' => $status,
], $status);
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
2. 在bootstrap/app.php 中注册
$app->register(App\Providers\ResponseMacroServiceProvider::class);