PHPackages                             wwjpackages/laravel-json-api - 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. wwjpackages/laravel-json-api

ActiveLibrary

wwjpackages/laravel-json-api
============================

提供统一返回格式和错误处理方式

1.0.2(5y ago)012MITPHPPHP ^7.0

Since Jan 2Pushed 5y ago1 watchersCompare

[ Source](https://github.com/ARSENAL91/laravel-json-api)[ Packagist](https://packagist.org/packages/wwjpackages/laravel-json-api)[ Docs](https://github.com/ARSENAL91/laravel-json-api.git)[ RSS](/packages/wwjpackages-laravel-json-api/feed)WikiDiscussions main Synced 1mo ago

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

laravel-json-api
================

[](#laravel-json-api)

主要是为了统一前后端 restful api 的返回格式标准，按照  定义返回指定的数据格式。

安装
--

[](#安装)

### 从composer安装

[](#从composer安装)

```
composer require wwjpackages/laravel-json-api
```

### 添加service provider

[](#添加service-provider)

#### in laravel:

[](#in-laravel)

在 `config/app.php` 中的 `providers` 添加

```
'providers' => [
    ...
    WwjPackages\JsonApi\Providers\LaravelServiceProvider::class,
]

```

#### in lumen:

[](#in-lumen)

在 `bootstrap/app.php` 中添加

```
$app->register(WwjPackages\JsonApi\Providers\LaravelServiceProvider::class);

```

### 发布配置

[](#发布配置)

php artisan vendor:publish --provider "WwjPackages\\JsonApi\\Providers\\LaravelServiceProvider"

使用
--

[](#使用)

### Controller

[](#controller)

添加一个 `trait` 给response，`ApiHelper`包含了4个常用的返回值和status code类型。

分别为 `content`, `noContent`, `created`, `accepted`

```
namespace App\Http\Controllers;

use WwjPackages\JsonApi\Traits\ApiHelper;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use ApiHelper;

    public function show()
    {
        // do something...
        return $this->content(['foo' => 'bar']);
    }

    public function create()
    {
        // do something...
        return $this->created();
    }
}
```

### Exception

[](#exception)

1、命令行创建一个新的 Exception 类

```
php artisan make:api-exception NotFoundException
```

2、根据标准修改 http 状态码

```
class NotFoundException extends BaseApiException
{
    protected $status = 404;
}
```

3、在 `config/errors.php` 中配置错误详情

```
'not_found_user' => [
    'code'   => 10003,
    'detail' => '未找到该用户 {name}',
],

```

4、抛出自定义异常

```
throw new NotFoundException('not_found_user', ['name' => 'xiaoming']);
```

5、重写 `app/Exceptions/Handler.php` 的`render`方法

```
function render($request, Exception $e)
    {
        $rendered = parent::render($request, $e);

        if (config('app.debug')) {
            return $rendered;
        }

        $status = $rendered->getStatusCode();
        $errors = [
            'code'   => 10000,
            'detail' => $e->getMessage(),
            'status' => $status,
        ];

        if ($e instanceOf \WwjPackages\JsonApi\Exceptions\BaseApiException) {
            $errors = $e->toArray();
            $status = $e->getStatus();
        }

        if ($e instanceOf \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
            $errors = array_merge([
                'title'  => 'not_found',
                'status' => 404,
            ], config('errors.not_found'));

            $status = 404;
        }

        if ($e instanceOf \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException) {
            $errors = array_merge([
                'title'  => 'method_not_allowed',
                'status' => 405,
            ], config('errors.method_not_allowed'));

            $status = 405;
        }

        return response()->json(['errors' => $errors], $status);
    }
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

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 ~193 days

Total

3

Last Release

1933d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8a4eeaafabdc93d11226f6c79952a82013cf1d901f1aba849a7c4080a9fdabd1?d=identicon)[ARSENAL91](/maintainers/ARSENAL91)

---

Top Contributors

[![ARSENAL91](https://avatars.githubusercontent.com/u/15209620?v=4)](https://github.com/ARSENAL91 "ARSENAL91 (1 commits)")

### Embed Badge

![Health badge](/badges/wwjpackages-laravel-json-api/health.svg)

```
[![Health](https://phpackages.com/badges/wwjpackages-laravel-json-api/health.svg)](https://phpackages.com/packages/wwjpackages-laravel-json-api)
```

###  Alternatives

[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[genealabs/laravel-model-caching

Automatic caching for Eloquent models.

2.4k4.8M26](/packages/genealabs-laravel-model-caching)[unisharp/laravel-filemanager

A file upload/editor intended for use with Laravel 5 to 10 and CKEditor / TinyMCE

2.2k3.3M73](/packages/unisharp-laravel-filemanager)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[mikebronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k127.1k1](/packages/mikebronner-laravel-model-caching)

PHPackages © 2026

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