承接 czim/laravel-jsonapi 相关项目开发

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

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

czim/laravel-jsonapi

最新稳定版本:1.5.4

Composer 安装命令:

composer require czim/laravel-jsonapi

包简介

Laravel JSON-API Base.

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status

JSON-API Base

Basic application elements for JSON-API projects.

Offers means for quickly scaffolding JSON-API compliance for Laravel applications.

This does NOT provide the means to set up the API or the means for user authorisation.

Disclaimer

This is very much a work in progress at this time. Interface- and other breaking changes may happen.
The old, discontinued version of this project is in the 0.9.5 branch.

Version Compatibility

Laravel Package
5.3 1.3
5.4 to 5.6 1.4
5.7 to 6.0 1.5

Note that version 1.5+ requires PHP 7.1.3+ and czim/laravel-dataobject 2.0+.

Changelog

View the changelog.

Installation

Via Composer

$ composer require czim/laravel-jsonapi

Add the JsonApiServiceProvider to your config/app.php:

Czim\JsonApi\Providers\JsonApiServiceProvider::class,

Publish the configuration file.

php artisan vendor:publish

Exceptions

In your App\Exceptions\Handler, change the render() method like so:

<?php public function render($request, Exception $exception) { if (is_jsonapi_request() || $request->wantsJson()) { return jsonapi_error($exception); } // ...

This will render exceptions thrown for all JSON-API (and JSON) requests as JSON-API error responses.

Middleware

To enforce correct headers, add the Czim\JsonApi\Http|Middleware\JsonApiHeaders middleware to the middleware group or relevant routes. You can do this by adding it to your App\Http\Kernel class:

<?php protected $middlewareGroups = [ 'api' => [ // ...  \Czim\JsonApi\Http\Middleware\RequireJsonApiHeader::class, ], ];

Note that this will block access to any consumers of your API that do not conform their HTTP header use to the JSON-API standard.

Documentation

Request Data

Request Query String Data

JSON-API suggests passing in filter and page data using GET parameters, such as:

{API URL}?filter[id]=13&page[number]=2 

This package offers tools for accessing this information in a standardized way:

Using the jsonapi_query() global helper function. This returns the singleton instance of Czim\JsonApi\Support\Request\RequestParser.

<?php // Get the full filter data associative array. $filter = jsonapi_query()->getFilter(); // Get a specific filter key value, if it is present (with a default fallback). $id = jsonapi_query()->getFilterValue('id', 0); // Get the page number. $page = jsonapi_query()->getPageNumber();

You can ofcourse also instantiate the request parser yourself to access these methods:

<?php // Using the interface binding ... $jsonapi = app(\Czim\JsonApi\Contracts\Support\Request\RequestQueryParserInterface::class); // Or by instantiating it manually ... $jsonapi = new \Czim\JsonApi\Support\Request\RequestQueryParser(request()); // After this, the same methods are available $id = $jsonapi->getFilterValue('id');

Request Body Data

For PUT and POST requests with JSON-API formatted body content, special FormRequests are provided to validate and access request body data: \Czim\JsonApi\Http\Requests\JsonApiRequest.

For POST requests where id may be omitted while creating a resource, use \Czim\JsonApi\Http\Requests\JsonApiRequest instead.

These classes may be extended and used as any other FormRequest class in Laravel.

There are also a global help functions jsonapi_request() and jsonapi_request_create(), that returns an instance of the relevant request class (and so mimics Laravel's request()).

Using this approach guarantees that requests are valid JSON-API by validating the input against a JSON Schema.

<?php // Get the root type of the object (which may be 'resource', 'error' or 'meta'). $rootType = jsonapi_request()->data()->getRootType(); // Get validated data for the current request. // This returns an instance of \Czim\JsonApi\Data\Root, which is a data object tree. $root = jsonapi_request()->data(); // You can check what kind of resource data is contained. if ( ! $root->hasSingleResourceData()) { // In this case, the request would either have no "data" key, // or it would contain NULL or an array of multiple resources. } elseif ($root->hasMultipleResourceData()) { // In this case, the request has a "data" key that contains an array of resources. } // Embedded data may be accessed as follows (for single resource). $resourceId = $root->data->id; $resourceType = $root->data->type; $attributeValue = $root->data->attributes->name; $relationType = $root->data->relationships['some-relationship']->data->type;

The request data tree for a single-resource request:

Request Data: Single Resource

For more information on the data object tree, see the Data classes.

Encoding

This package offers an encoder to generate valid JSON-API output for variable input content.

With some minor setup, it is possible to generate JSON output according to JSON-API specs for Eloquent models and errors.

Eloquent models, single, collected or paginated, will be serialized as JSON-API resources.

More information on encoding and configuring resources.

Custom Encoding & Transformation

To use your own transformers for specific class FQNs for the content to be encoded, map them in the jsonapi.transform.map configuration key:

<?php 'map' => [ \Your\ContentClassFqn\Here::class => \Your\TransformerClassFqn\Here::class, ],

This mapping will return the first-matched for content using is_a() checks. More specific matches should be higher in the list.

As a last resort, you can always extend and/or rebind the Czim\JsonApi\Encoder\Factories\TransformerFactory to provide your own transformers based on given content type.

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

czim/laravel-jsonapi 适用场景与选型建议

czim/laravel-jsonapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 62.2k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「laravel」 「JSON-API」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 czim/laravel-jsonapi 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 62.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04