adithwidhiantara/laravel-response-formatter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

adithwidhiantara/laravel-response-formatter

最新稳定版本:1.0.0

Composer 安装命令:

composer require adithwidhiantara/laravel-response-formatter

包简介

Simple JSON response formatter for Laravel

README 文档

README

Package sederhana untuk memformat response JSON di Laravel dengan struktur yang konsisten.

Instalasi

Tambahkan package ke project Laravel kamu:

composer require adithwidhiantara/laravel-response-formatter

Jika belum rilis ke Packagist, gunakan repository lokal atau VCS (GitHub/GitLab) sesuai kebutuhan.

Cara Menggunakan

Import class ResponseFormatter:

use Adithwidhiantara\LaravelResponseFormatter\ResponseFormatter;

Success Response

return ResponseFormatter::success(
    data: ['id' => 1, 'name' => 'John Doe'],
    message: 'Data retrieved successfully',
    status: 'success',
    code: \Symfony\Component\HttpFoundation\Response::HTTP_OK
);

Output JSON:

{
  "status": "success",
  "message": "Data retrieved successfully",
  "data": {
    "id": 1,
    "name": "John Doe"
  }
}

Error Response

return ResponseFormatter::error(
    data: null,
    message: 'User not found',
    status: 'error',
    code: \Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND
);

Output JSON:

{
  "status": "error",
  "message": "User not found",
  "data": null
}

Contoh Penggunaan di Controller

use App\Http\Controllers\Controller;
use Adithwidhiantara\LaravelResponseFormatter\ResponseFormatter;
use App\Models\User;
use Symfony\Component\HttpFoundation\Response;

class UserController extends Controller
{
    public function show($id)
    {
        $user = User::find($id);

        if (!$user) {
            return ResponseFormatter::error(
                message: 'User not found',
                code: Response::HTTP_NOT_FOUND
            );
        }

        return ResponseFormatter::success(
            data: $user,
            message: 'User retrieved successfully'
        );
    }
}

License

MIT License

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-17