therour/your-json-response

json响应骨架的修饰器

0.0.3 2018-11-02 10:42 UTC

This package is auto-updated.

Last update: 2024-09-29 05:20:51 UTC


README

安装

通过Composer

composer require therour/your-json-response

对于Laravel 5.4及以下版本 将服务提供者添加到你的Laravel项目的 config/app.php

'providers' => [
    ...
    Therour\YourJsonResponse\ServiceProvider::class,
] 

配置

发布配置文件

php artisan vendor:publish --provider=Therour\YourJsonResponse\ServiceProvider

配置文件位于你的Laravel配置目录,文件名为 yjresponse.php

使用方法

本包包含名为 ok 的骨架,你可以在配置 yjresponse.php 中编辑骨架

return response()->ok($data, $message = 'success', $code = 200);
// or
return response()->yourNamedSkeleton($data, $message = 'success', $code = 200);

创建骨架

打开配置文件 yjresponse.php

使用 codemessagedatatypemeta_page 作为骨架的值来放置它

例如

'skeleton' => [
    ...
    // 'created' is the name of skeleton, and its value is the skeleton
    'created' => [
        'status' => [
            'code',
            'message'
        ],
        'result' => 'data'
    ]
],

在同一文件中,也定义默认值

'defaults' => [
    ...
    'created' => [
        'code' => 201,
        'message' => 'Succesfully create the object'
    ]
]

使用你的自定义骨架

return response()->created($product);

结果

{
    "status": {
        "code": 201,
        "message": "Succesfully create the object"
    },
    "result": {
        "name": "Product X",
        "price": 6000,
        "weight": 500
    }
}