定制 ferdous-ahmed-khan/laravel-api-response 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ferdous-ahmed-khan/laravel-api-response

最新稳定版本:v1.0.1

Composer 安装命令:

composer require ferdous-ahmed-khan/laravel-api-response

包简介

A Laravel package providing standardized JSON response helpers for success and error handling, with functions for quick, consistent API responses.

README 文档

README

This guide will walk you through the installation and setup process for integrating the package into your Laravel project.

JSON Response Structure

{
   "status": true|false,
   "message": "Your message here",
   "data": { /*optional data */ },
   "errors": { /* optional error details*/ }
}

Requirements

  • Laravel 8.x or higher
  • PHP 7.4+ (or higher, depending on the package requirements)
  • Composer

Installation

To install the package, follow these steps:

1. Install via Composer

Run the following command in your Laravel project root directory to install the package:

composer require ferdous-ahmed-khan/laravel-api-response

2. Open config/app.php

In the providers array, add the following line to register the service provider:

'providers' => [
    // Other Service Providers...

    FerdousAhmedKhan\LaravelApiResponse\ResponseHelperServiceProvider::class,
],

In the aliases array, add the following line to create an alias for the ResponseHelper facade:

'aliases' => [
    // Other Aliases...

    'ApiResponse' => FerdousAhmedKhan\LaravelApiResponse\ResponseHelperFacade::class,
],

3. Usage

Once the package is installed, you can start using its features. For example, to use the

use ApiResponse;

success()

Generates a successful response with HTTP status 200.

return ApiResponse::success('Operation successful', $data);

deleted()

Generates a response for successful deletions with HTTP status 200

return ApiResponse::deleted('Record deleted successfully', $data);

created()

Generates a response for successful creation with HTTP status 201

return ApiResponse::created('Resource created successfully', $data);

badRequest()

Generates a response for a bad request with HTTP status 400.

return ApiResponse::badRequest('Invalid request data', $errorDetails);

failed()

Generates a generic failure response with HTTP status 400.

return ApiResponse::failed('Operation failed', $errorDetails);

unauthorized()

Generates a response for forbidden access with HTTP status 403.

return ApiResponse::forbidden('Access is forbidden', $errorDetails);

forbidden()

Generates a response for forbidden access with HTTP status 403.

return ApiResponse::forbidden('Access is forbidden', $errorDetails);

notFound()

Generates a response for resources not found with HTTP status 404.

return ApiResponse::notFound('Resource not found', $errorDetails);

validationFailed()

Generates a response for validation failure with HTTP status 422.

return ApiResponse::validationFailed('Validation failed', $validationErrors);

internalServerError()

Generates a response for server errors with HTTP status 500.

return ApiResponse::internalServerError('An error occurred', $errorDetails);

custom()

Generates a custom response with a specified HTTP status code.

return ApiResponse::custom(true, 202, 'Request accepted', $data, null);

Each function returns a JsonResponse object with a consistent structure:

{
   "status": true|false,
   "message": "Your message here",
   "data": { /*optional data */ },
   "errors": { /* optional error details*/ }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-08