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

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

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

tequilarapido/api-response

Composer 安装命令:

composer require tequilarapido/api-response

包简介

A simple way to return well formatted json responses in a Laravel application.

README 文档

README

A simple way to return well formatted json responses in a Laravel application.

Latest Version on Packagist Software License Build Status StyleCI Quality Score Code Coverage

Laravel Translation Sheet

Installation

You can install the package using composer

$ composer require tequilarapido/api-response

Add the service provider

Tequilarapido\ApiResponse\ApiResponseServiceProvider::class 

Usage

This package comes with a helper function api_response(), as sugar syntax to using app(Tequilarapido\ApiResponse\ApiResponse::class)

Return a response from an item (Array / Associative array / Object/)

return api_response()->item(['result`' => 'success'])

Result :

    {
        "data":
        {   
            "result":
            "success"
        }
    }

Return a response from a collection

return api_response()->item(collect(['Apple', 'Orange', 'Kiwi']))

Result :

    {
        "data":
            {   
                "Apple",
                "Orange",
                "Kiwi",
            }
    }

Return a response from a paginated collection

return api_response()->item(collect(['Apple', 'Orange', 'Kiwi']))

Result :

    {
        "data": [1, 2, 3],
        "meta": {
            "pagination": {
                "count": 3,
                "current_page": 1,
                "links": {
                    "next": "/?page=2"
                },
                "per_page": 3,
                "total": 15,
                "total_pages": 5
            }
         }
    }

Transformers

You can use transformers along with item(), collection(), and paginatedCollection methods to transform results before returning the responses.

To learn more about the concept behind transformers, please read the League\Fractal documentation.

Put in a simple way, a Transformer class - must extends League\Fractal\TransformerAbstract - and contains a method transform() that take the raw value/object as an argument and transform it the way we want.

Let's assume that we need to return an api response containing a collection of books retreived from database, and we want to format/enhance/restrict results.

class BookTransformer extends TransformerAbstract
{
    public function transform($book)
    {
        return [
            'id' => (int)$book->id,
            'title' => strtoupper($book->title),
            'price' => '$' . $book->price,
            'published_at' => $book->published_at->format('d/m/Y'),
            'url' => 'https://store.books.com/books/' . $book->id
        ];
    }
}
Route::get('/books', function () {

    // ie. Book::all()
   $books = collect([
       (object)['title' => 'An awesome book', 'id' => '1', 'price' => 10, 'published_at' => Carbon::createFromFormat('Y-m-d', '2016-10-01')],
       (object)['title' => 'A second awesome book', 'id' => '2', 'price' => 100, 'published_at' => Carbon::createFromFormat('Y-m-d', '2016-10-02')],
   ]);

   return api_response()->collection($books, new BookTransformer);
});

Result:

{
    "data": [
        {
            "id": 1,
            "price": "$10",
            "published_at": "01/10/2016",
            "title": "AN AWESOME BOOK",
            "url": "https://store.books.com/books/1"
        },
        {
            "id": 2,
            "price": "$100",
            "published_at": "02/10/2016",
            "title": "A SECOND AWESOME BOOK",
            "url": "https://store.books.com/books/2"
        }
    ]
}

Attaching cookies

For item(), collection(), and paginatedCollection methods the returned result is a built Illuminate\Http\Response object.

To be able to attach cookies you need to instruct api_response() methods to not build the response by setting the $built argument to false, attach the cookie and then build the response.

return api_response()
    ->item(['result`' => 'success'], null, null, false)
    ->withCookie(new \Symfony\Component\HttpFoundation\Cookie('name', 'value'))
    ->build();

Attaching headers

For item(), collection(), and paginatedCollection methods the returned result is a built Illuminate\Http\Response object.

To be able to attach headers you need to instruct api_response() methods to not build the response by setting the $built argument to false, attach the header and then build the response.

return api_response()
    ->item(['result`' => 'success'], null, null, false)
    ->withHeader('X-CUSTOM', 'customvalue')
    ->build();

Other useful methods

Method Usage
api_response()->noContent() Return an empty response with 204 No Content header.
api_response()->errorNotFound() Return a 404 Not found.
api_response()->errorBadRequest() Return a 404 Bad Request.
api_response()->errorForbidden() Return a 403 Forbidden.
api_response()->errorUnAUthorized() Return a 401 Unauthorized.
api_response()->errorInternal() Return a 500 Internal Error.
api_response()->error() Return a more customizable error (Accept MessageBag instance, staus code ...)

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email :author_email instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

tequilarapido/api-response 适用场景与选型建议

tequilarapido/api-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.34k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tequilarapido/api-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tequilarapido/api-response 我们能提供哪些服务?
定制开发 / 二次开发

基于 tequilarapido/api-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-08