pixelindustries / laravel-jsonapi
Laravel JSON-API 基础。
Requires
- php: >=5.6.0
- czim/laravel-dataobject: ^1.0
- justinrainbow/json-schema: ^5.2
- myclabs/php-enum: ^1.5
Requires (Dev)
- mockery/mockery: 0.9.*
- orchestra/testbench: 3.3.*
- phpunit/phpunit: 5.*
- scrutinizer/ocular: ~1.1
This package is auto-updated.
Last update: 2020-01-19 19:08:20 UTC
README
此包已被废弃!
请使用 czim/laravel-jsonapi 代替。
JSON-API 基础
JSON-API 项目的基本应用元素。
提供快速构建 Laravel 应用程序 JSON-API 兼容性的方法。
这 不 提供设置 API 或用户认证的方法。
版本兼容性
Laravel | 包 |
---|---|
5.3.x | ? |
5.4.x | ? |
安装
通过 Composer
$ composer require pixelindustries/laravel-jsonapi
将 JsonApiServiceProvider
添加到您的 config/app.php
Pixelindustries\JsonApi\Providers\JsonApiServiceProvider::class,
发布配置文件。
php artisan vendor:publish
异常
在您的 App\Exceptions\Handler
中,将 render()
方法更改为以下内容
<?php public function render($request, Exception $exception) { if (is_jsonapi_request() || $request->wantsJson()) { return jsonapi_error($exception); } // ...
这将使所有 JSON-API(和 JSON)请求抛出的异常以 JSON-API 错误响应的形式渲染。
中间件
为了强制正确的头信息,请将 Pixelindustries\JsonApi\Http|Middleware\JsonApiHeaders
中间件添加到中间件组或相关路由。您可以通过将其添加到您的 App\Http\Kernel
类来实现此操作
<?php protected $middlewareGroups = [ 'api' => [ // ... \Pixelindustries\JsonApi\Http\Middleware\RequireJsonApiHeader::class, ], ];
请注意,这将 阻止 任何不遵守 JSON-API 标准的 HTTP 头信息使用规范的 API 消费者访问。
文档
请求数据
请求查询字符串数据
JSON-API 建议使用 GET 参数传递过滤和页面数据,例如
{API URL}?filter[id]=13&page[number]=2
此包提供工具以标准化方式访问此信息
使用全局辅助函数 jsonapi_query()
。此函数返回 Pixelindustries\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();
当然,您也可以自己实例化请求解析器来访问这些方法
<?php // Using the interface binding ... $jsonapi = app(\Pixelindustries\JsonApi\Contracts\Support\Request\RequestQueryParserInterface::class); // Or by instantiating it manually ... $jsonapi = new \Pixelindustries\JsonApi\Support\Request\RequestQueryParser(request()); // After this, the same methods are available $id = $jsonapi->getFilterValue('id');
请求体数据
对于具有 JSON-API 格式化正文内容的 PUT
和 POST
请求,提供了一个特殊的 FormRequest 来验证和访问请求体数据 (\Pixelindustries\JsonApi\Http\Requests\JsonApiRequest)。
此类可以扩展并用作 Laravel 中的任何 FormRequest 类。
还有一个全局帮助函数 jsonapi_request()
,它返回此类的实例(从而模仿 Laravel 的 request()
)。
<?php // Get validated data for the current request $jsonApiType = jsonapi_request()->getType(); $jsonApiId = jsonapi_request()->getId();
编码
此包提供了一个编码器,用于生成有效 JSON-API 输出,以适应各种输入内容。
通过一些简单的设置,可以生成符合 JSON-API 规范的 JSON 输出,用于 Eloquent 模型和错误。
单个、集合或分页的 Eloquent
模型将被序列化为 JSON-API 资源。
自定义编码与转换
要为要编码的内容使用特定类的FQNs的自定义转换器,请在jsonapi.transform.map
配置键中进行映射。
<?php 'map' => [ \Your\ContentClassFqn\Here::class => \Your\TransformerClassFqn\Here::class, ],
此映射将使用is_a()
检查返回第一个匹配的内容。更具体的匹配项应排在列表中较高位置。
作为最后的手段,您可以扩展和/或重新绑定Pixelindustries\JsonApi\Encoder\Factories\TransformerFactory
来提供基于给定内容类型自己的转换器。
贡献
有关详细信息,请参阅贡献指南。
鸣谢
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。