Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alicfeng/api-response


https://github.com/alicfeng/api-response

Last synced: 20 days ago
JSON representation

Awesome Lists containing this project

README

        



ApiResponse



A Laravel Plugin For Manage API Response Package


It could make your api standard as well as Improve development efficiency




Build Status


Latest Stable Version


Total Downloads


License

## 能力

1. 统一动作标准化影响体结构
2. 支持响应自定义报文加解密

## 安装

```shell
composer require alicfeng/api-response -vvv

php artisan vendor:publish --provider="Samego\Response\ServiceProvider\ServiceProvider"
```

## 配置

```php
[
'code' => 'code',
'message' => 'message',
'data' => 'data',
'request_id' => 'request_id',
],

// Default Header simple:Content-Type => application/json
'header' => [
'Content-Type' => 'application/json',
],

/*Package encrypt Setting*/
'crypt' => [
'instance' => \Samego\Response\Service\CryptService::class,
'method' => 'aes-128-ecb',
'password' => '1234qwer',
],

/*Log*/
'log' => [
'log' => true,
],

// Translate
'translate' => [
'model' => true,
'instance' => \Samego\Response\Service\Translation::class,
],

// Runtime model
'runtime' => [
'trace' => [
'request' => true,
'response' => false,
'filter_uri' => [
],
],
],

// Debug model setting
'debug' => false,
];

```

## 使用

业务层基类定义

```PHP
namespace App\Services;

use App\Enums\Application\CodeEnum;
use Illuminate\Contracts\Routing\ResponseFactory;
use Samego\Response\Facades\ApiResponse;

/**
* Class Service
* 业务层基类.
* @version 1.0.0
* @author AlicFeng
*/
class Service
{
/**
* @function result
* @description 响应业务处理结果调用
* @param array $code_enum 业务编码枚举
* @param array $data 业务结果数据
* @param int $status_code 协议状态编码
* @param array $headers 协议头部信息
* @return ResponseFactory
* @author AlicFeng
*/
public function result(array $code_enum, $data = [], int $status_code = 200, array $headers = [])
{
return ApiResponse::result($code_enum, $data, $status_code, $headers);
}

/**
* @function success
* @description 响应业务成功处理结果调用
* @param array $data 业务结果数据
* @param array $code_enum 业务编码枚举
* @param int $status_code 协议状态编码
* @param array $headers 协议头部信息
* @return ResponseFactory
* @author AlicFeng
*/
public function success($data = [], array $code_enum = CodeEnum::SUCCESS, int $status_code = 200, array $headers = [])
{
return $this->result($code_enum, $data, $status_code, $headers);
}

/**
* @function failure
* @description 响应业务失败处理结果调用
* @param array $code_enum 业务编码枚举
* @param array $data 业务结果数据
* @param int $status_code 协议状态编码
* @param array $headers 协议头部信息
* @return ResponseFactory
* @author AlicFeng
*/
public function failure(array $code_enum = CodeEnum::FAILURE, $data = [], int $status_code = 200, array $headers = [])
{
return $this->result($code_enum, $data, $status_code, $headers);
}
}
```

具体业务层使用

```php
namespace App\Services\Demo;

use App\Services\Service;

/**
* Class HelloService
* 模板示例业务层 项目上请删除 TODO.
* @version 1.0.0
* @author AlicFeng
*/
class HelloService extends Service
{
public function __construct()
{
parent::__construct();
}

public function printer(string $name)
{
$data = compact('name');

return $this->success($data);
}
}
```