PHPackages                             keal/laravel-output - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. keal/laravel-output

ActivePhp[API Development](/categories/api)

keal/laravel-output
===================

Laravel standard output for restful api

v0.1.2(9y ago)042MITPHPPHP &gt;=5.6.4

Since Feb 16Pushed 9y ago1 watchersCompare

[ Source](https://github.com/caikeal/standard-output)[ Packagist](https://packagist.org/packages/keal/laravel-output)[ RSS](/packages/keal-laravel-output/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (10)Used By (0)

关于`Laravel`项目规范`Api`输出和错误提示（测试版本）
=================================

[](#关于laravel项目规范api输出和错误提示测试版本)

安装
==

[](#安装)

建议使用Laravel &gt;= 5.1.\* , PHP &gt;= 5.6

`composer require keal/laravel-output`

使用
==

[](#使用)

注册ServiceProvider
-----------------

[](#注册serviceprovider)

在`app.php`中加入`Caikeal\Output\Provider\OutputServiceProvider::class`

引入错误处理
------

[](#引入错误处理)

需要在`Exceptions/Handler.php`中引入`Caikeal\Output\Exceptions\HandlerTrait`该方法， 然后在改写下`render`方法，如下：

```
    public function render($request, Exception $exception)
    {
        if ($request->expectsJson()) {
            return $this->handlerApi($exception);
        }

        return parent::render($request, $exception);
    }

```

引入返回值基类
-------

[](#引入返回值基类)

在所有`Controller`的基类中引入该方法，如下：

```
    protected function response()
    {
        return app('output.response');
    }

```

返回方法
----

[](#返回方法)

在返回时可以用该方法代替response返回：`$this->reponse()`的方法 所有方法如下：

- 正确值返回：

    - model层，且带有transformer验证的单列数据：`$this->response()->item([Model], [Transformer]);`
    - model层，且带有transformer验证的多列数据：`$this->response()->collection([Model], [Transformer]);`
    - model层，且带有transformer验证的分页数据：`$this->response()->paginator([Model], [Transformer]);`
    - 任意：`$this->response()->array([Model/Array]);`

    返回值样式：

    ```
      {
        "data": {
          "name": "keal"
        },
        "meta": {
          "type": "seller"
        }
      }

    ```
- 错误值返回：

    - 404错误：`$this->response()->errorNotFound([$message], [$code])`
    - 400错误：`$this->response()->errorBadRequest([$message], [$code])`
    - 403错误：`$this->response()->errorForbidden([$message], [$code])`
    - 500错误：`$this->response()->errorInternal([$message], [$code])`
    - 401错误：`$this->response()->errorUnauthorized([$message], [$code])`
    - 405错误：`$this->response()->errorMethodNotAllowed([$message], [$code])`
    - 任意错误：`$this->response()->error([$message], [$statusCode], [$code])`

    返回值样式：

    ```
      {
        "code": "401000",
        "status_code": "401",
        "message": "Wrong."
      }

    ```

`Transformer`的数据格式转换
--------------------

[](#transformer的数据格式转换)

在所有`Transformer`类中必须继承`League\Fractal\TransformerAbstract`，使用如下：

```
    namespace App\Transformers;

    use App\User;
    use League\Fractal\TransformerAbstract;

    class UserTransformer extends TransformerAbstract
    {
        public function transform(User $data){
            return [
                'id'=>intval($data['id']),
                'name'=>ucfirst($data['name'])
            ];
        }
    }

```

`Artsian`创建`Transformer`类
-------------------------

[](#artsian创建transformer类)

创建Transformer：`php artisan make:transformer User`

附带Model名：`php artisan make:transformer User -m User`

或者`php artisan make:transformer User --model=User`

或者`php artisan make:transformer User --model=Model/User` 带位置，命令会自动转化成空间名，并在开头添加`App`

感谢
==

[](#感谢)

主要借鉴和引用了[Dingo/api](https://github.com/dingo/api.git)和[league/fractal](https://github.com/thephpleague/fractal.git)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

9

Last Release

3350d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/90054c6ceecc7344184d557c5100f1cded77eb8b52964a65fecb7ae4547a9d17?d=identicon)[caikeal](/maintainers/caikeal)

---

Top Contributors

[![caikeal](https://avatars.githubusercontent.com/u/11376690?v=4)](https://github.com/caikeal "caikeal (19 commits)")

---

Tags

laravellaravel-packageresponserestful-apirestful-api-php

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/keal-laravel-output/health.svg)

```
[![Health](https://phpackages.com/badges/keal-laravel-output/health.svg)](https://phpackages.com/packages/keal-laravel-output)
```

###  Alternatives

[yajra/laravel-datatables-fractal

Laravel DataTables Fractal Plugin.

966.9M29](/packages/yajra-laravel-datatables-fractal)[esign/laravel-conversions-api

A laravel wrapper package around the Facebook Conversions API

69145.4k](/packages/esign-laravel-conversions-api)[didww/didww-api-3-php-sdk

PHP SDK for DIDWW API 3

1218.2k](/packages/didww-didww-api-3-php-sdk)[surface/laravel-webfinger

A Laravel package to create an ActivityPub webfinger.

113.8k](/packages/surface-laravel-webfinger)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
