承接 swapnil/api-response-creator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

swapnil/api-response-creator

Composer 安装命令:

composer require swapnil/api-response-creator

包简介

Package to give common exit point to handle api responses with multiple error codes.

README 文档

README

Maintained by @anshdesire, Laravel api reponse generatorin the laravel 5 application. and published to packagist(https://packagist.org/packages/swapnil/api-response-creator) .

Installation

You can install the package through Composer.

composer require swapnil/api-response-creator

You must install this service provider. Make this the very first provider in list.

```php

'providers' => [
    'Swapnil\ApiResponse\ServiceProvider',
    //...
];

Publish

php artisan vendor:publish

Use

return \ApiResponse::respond($data, $status = true,$headers = []);

return \ApiResponse::setStatusCode(200);

return  \ApiResponse::setErrorCode(200); //with error code given in cofig.
 
return \ApiResponse::getStatusCode();
 
return \ApiResponse::getStatusCodeList()[404];

return \ApiResponse::respondWithPagination(Paginator $items, $data, $status= true); // if pagination data needs to be send then 
 
Method chaining can also be used here.
 
 return \ApiResponse::setStatusCode(200)->ApiResponse::respondWithPagination(Paginator $items, $data, $status= true);
 
 
 
 
 
 Standard json response structure
 
 {
  "status_code": 422,
  "errors": [
    {
      "error_message": "Validation error.",
      "error_code": 2004,
      "error_data": {
        "user_name": [
          "The user name has already been taken."
        ],
        "mobile": [
          "The mobile has already been taken."
        ]
      }
    }
  ],
  "data": [],
  "paginator": [],
  "success": false,
  "success_message": "Unprocessed entity"
}




All kind of error codes and error messages can be configured in swapnil.api-response.php config file.


    'status_code' => 200,
    'status_code_list' => [
      200 => 200,
      422 => 422,
      201 => 201,
      404 => 404,
      500 => 500,
    ],
    'status_code_messages' => [
          200 => 'Request successfully processed.',
          422 => 'Unprocessed entity',
          201 => 'Created.',
          404 => '',
          500 => 'Internal Server Error.',
    ],
    'errors_list' => [ // configurable
    '2001' => [
      'error_message' => 'User not found or wrong credentials.',
      'error_code' => 2001,
          'error_data' => '',
        ],

    '2002' => [
      'error_message' => '2002',
      'error_code' => 2002,
          'error_data' => '',
        ],

    '2003' => [
          'error_message' => 'Unable to create.',
          'error_code' => 2003,
          'error_data' => '',
        ],

    '2004' => [
          'error_message' => 'Validation error.',
          'error_code' => 2004,
          'error_data' => '',
      ]
     ],
  
  
  
  

License

The MIT License (MIT). Please see LICENSE for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-18