ymigval/laravel-sl-response

一个用于简化API响应格式化和管理的Laravel包,确保一致性和遵守API标准。轻松处理错误并自定义响应格式。

v1.0.1 2023-11-13 15:32 UTC

This package is auto-updated.

Last update: 2024-09-22 02:21:51 UTC


README

ymigval/laravel-sl-response 包简化了Laravel应用程序中的API响应管理。它允许您轻松地构建和格式化API响应,确保一致性和遵守API标准。

主要特性

  • 一致的响应:生成具有标准化结构(包括状态码、消息和数据)的API响应,为您的API提供统一和专业的外观。

  • 错误处理:通过详细的错误消息、适当的状态码和符合最佳实践的响应格式,高效地管理错误响应。

  • 自定义:根据项目的具体需求调整响应格式,以适应各种API要求。

  • Laravel集成:与Laravel无缝集成,通过额外功能增强Laravel的响应系统。

  • 时间效率:减少一致API响应的开发工作量,让您能够专注于应用程序的核心功能,而不是响应格式。

ymigval/laravel-sl-response 包使您能够构建和维护符合行业标准的专业、可靠的API。无论您是在开发新API还是改进现有API,此包都是Laravel工具箱中不可或缺的补充。

安装

您可以通过Composer安装此包

composer require ymigval/laravel-sl-response

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="slresponse"

使用方法

来自路由或控制器方法的响应。

use Ymigval\LaravelSLResponse\Facades\SLResponse;
use Ymigval\LaravelSLResponse\Exceptions\SLException;

// Collection of users
return SLResponse::ok(User::all());

// A User resource
return SLResponse::ok(new UserResource(User::find(1)));

// If you want to disable the outermost wrapping.
return SLResponse::ok(UserResource::collection(User::paginate(10)))
        ->withoutWrapping();

// Attach additional messages.
return SLResponse::ok(User::find(6)->isAdmin())
        ->withMessage("This user is an admin");

// Return an error response.
return SLResponse::error('Limit exceeded');

// Additionally, you can add an error code as the second argument.
return SLResponse::error('Limit exceeded', '3748');

// Throw an exception
throw new SLException('This is a message.');

变更日志

有关最近更改的更多信息,请参阅变更日志

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件