百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

api-response

> 编程语言
开源

一个简单的包,可在您的 API 中正确处理响应。

372 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

一个简单的包,可在您的 API 中正确处理响应。

api-response

Simple package to handle response properly in your API. This package uses Fractal and is based on Build APIs You Won't Hate book.

Install

Via Composer

bash
$ composer require ellipsesynergie/api-response

Requirements

Version Branch PHP Laravel
2.x master 8.3 - 8.5 11 - 13
1.x 1.x 8.1 - 8.3 8 - 10

Version 2.x removed Lumen support, since Lumen is no longer maintained. If you need Lumen, stay on 1.x.

Install in Laravel

The service provider is auto-discovered. If you have disabled package discovery, add this following service provider to your config/app.php file.

php
EllipseSynergie\ApiResponse\Laravel\ResponseServiceProvider::class

Install in your favorite framework or vanilla php

This package can be used in any framework or vanilla php. You simply need to extend EllipseSynergie\ApiResponse\AbstractResponse and implement the withArray() method in your custom class. You can take a look at EllipseSynergie\ApiResponse\Laravel\Response::withArray() for an example.

You will also need to instantiate the response class with a fractal manager instance.

php
// Instantiate the fractal manager
$manager = new \League\Fractal\Manager;

// Set the request scope if you need embed data
$manager->parseIncludes(explode(',', $_GET['include']));

// Instantiate the response object, replace the class name by your custom class
$response = new \EllipseSynergie\ApiResponse\AbstractResponse($manager);

For more options related to the fractal manager, you can take a look at the official Fractal website

Example inside Laravel controller

…

Ouput example

One book

json
{
    "data": {
        "id": 1,
        "title": "My name is Bob!.",
        "created_at": {
            "date": "2014-03-25 18:54:18",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "updated_at": {
            "date": "2014-03-25 18:54:18",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "deleted_at": null
    }
}

Collection of books

…

Error

json
{
    "error": {
        "code": "GEN-NOT-FOUND",
        "http_code": 404,
        "message": "Book Not Found"
    }
}

Testing the package

bash
$ phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

  • Maxime Beaudoin
  • Phil Sturgeon
  • All Contributors

License

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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言