trihydera / res
请求的响应处理器
v1.1
2024-07-19 20:53 UTC
This package is auto-updated.
Last update: 2024-09-28 15:49:45 UTC
README
一个处理JSON和XML响应的辅助工具,适用于成功和错误场景,预设了常见的错误以提高效率。
最初作为我的API项目的内部库的一部分,但后来将其独立出来,以便更容易维护并在其他项目中使用。
使用方法
入门指南
使用 Composer 安装此包。
composer require trihydera/res
对于 Json
,使用 JsonResponse
来创建响应实例。
use Trihydera\Res\JsonResponse; // Create an instance of JsonResponse $response = new JsonResponse();
对于 Xml
,只需使用 XmlResponse
。
use Trihydera\Res\XmlResponse; // Create an instance of XmlResponse $response = new XmlResponse();
成功示例
返回一个包含 value
键的 array
,表示请求成功,例如 Its working
。
$response->success([ 'value' => 'Its working' ]);
错误示例
在请求上响应一个通用错误
$response->error('Something went wrong', '200');
使用预设的错误来加快速度,并且还可以使用自己的消息,如 API资源未找到
。
// Example usage of the useError method $response->useError('NotFound'); // Example usage of the useError method with your own message $response->useError('NotFound', 'API resource not found');
错误预设
- 参数
- 方法
- NotFound
- InternalError
- 授权