chatbox-inc / lumen-restapi
关于此包最新版本(0.0.2)的许可信息不可用。
0.0.2
2016-10-01 08:14 UTC
Requires (Dev)
- laravel/lumen: ^5.3
- peridot-php/peridot: ^1.18
This package is auto-updated.
Last update: 2024-09-13 19:00:05 UTC
README
Rest API 应用程序创建用中间件
统一响应生成和错误处理。
功能
- RestAPI 响应格式化的一元化
- 内置ExceptionHandler
用法
默认情况下,假设使用Exception Handler
$app->singleton(\Illuminate\Contracts\Debug\ExceptionHandler::class,\App\Exceptions\Handler::class);
将中间件应用于目标路由两次
请注意中间件的设置顺序。
$app->group([
"middleware" => [
\Chatbox\RestAPI\Http\Middleware\HttpExceptionHandler::class,
\Chatbox\RestAPI\Http\Middleware\APIResponseHandler::class
]
],function($router){
$router->get("/api/status",function(){
return [];
});
$router->get("/api/missing",function(){
abort(404);
});
$router->get("/api/error",function(){
throw new \Exception();
});
});
扩展
所有异常都会通过HttpRequest抛出一次,因此从那里处理会更方便。
当抛出400系列错误异常时,使用HttpExcepiton继承将进行特殊处理。
在操作Response时
扩展\Chatbox\RestAPI\Http\Response
在操作Exceptions时
扩展\Chatbox\RestAPI\Exceptions\Handler