realpage/json-api-for-lumen

该软件包已被废弃,不再维护。未建议替代软件包。

Lumen 助手,有助于轻松实现 json-api 标准

v1.4.1 2018-01-05 20:37 UTC

This package is not auto-updated.

Last update: 2019-03-14 23:37:03 UTC


README

Lumen 助手,有助于轻松实现 json-api 标准

Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Code Coverage

安装

通过 Composer

$ composer require realpage/json-api-for-lumen

Lumen

您可以在 bootstrap/app.php 中注册服务提供者

$app->register(RealPage\JsonApi\Lumen\ServiceProvider::class);

使用方法

中间件

强制媒体类型

满足 json-api 文档中“服务器责任”部分的规范。这应该包装所有符合 json-api 规范的端点。

您可以在 bootstrap/app.php 中注册中间件

$app->routeMiddleware([
    'json-api.enforce-media-type' => RealPage\JsonApi\Middleware\EnforceMediaType::class,
]);

然后您可以在您的 routes.php 文件中使用该中间件

$app->get('quotes', ['middleware' => 'json-api.enforce-media-type', function () {
  //
}]);

验证

使用 Laravel 的验证功能来验证您的请求,并且此软件包将处理将失败的验证转换为有效的 json-api 响应。

将此条目添加到您的异常处理程序

use Illuminate\Http\Response;
use Neomerx\JsonApi\Encoder\Encoder;
use Neomerx\JsonApi\Exceptions\JsonApiException;
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;

// automatically encode exceptions as valid json-api json responses
if ($e instanceof JsonApiException) {
    return new Response(Encoder::instance()->encodeErrors($e->getErrors()), $e->getHttpCode(), [
            'Content-Type' => MediaTypeInterface::JSON_API_MEDIA_TYPE,
        ]);
}

只需扩展几个类即可

use RealPage\JsonApi\Validation\ValidatesRequests;

class MyValidator extends ValidatesRequests
{
    public function rules() : array
    {
        return array_merge(parent::rules(), [
            'data.attributes.name' => 'required',
        ]);
    }
    
    public function messages() : array
    {
        return array_merge(parent::messages(), [
            'data.attributes.name.required' => 'A name is required',
        ]);
    }
}
use RealPage\JsonApi\Requests\Request;
use RealPage\JsonApi\Validation\ValidatesRequests;

class MyRequest extends Request
{
    public function validator() : ValidatesRequests
    {
        return new MyValidator();
    }
}

在控制器中,您只需对请求调用验证即可

    public function store(MyRequest $request)
    {
        $request->validate();
        
        // do stuff
    }

编码器

配置 Neomerx\JsonApi\Encoder\Encoder 实例以编码您的 JSONAPI 响应。

您可以在 config/json-api.php 中如此配置默认编码器

<?php
return [
    'media-type' => 'application/vnd.api+json',
    'schemas' => [
        App\Models\MyModel::class => App\Schemas\MySchema::class,
    ],
    'jsonapi' => true,
    'encoder-options' => [
        'options' => JSON_PRETTY_PRINT,
    ],
    'meta' => [
        'apiVersion' => '1.1',
    ],
];

然后您可以在控制器类中检索编码器

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use RealPage\JsonApi\EncoderService;
use App\Model\MyModel;

class MyController extends ApiController
{
    protected $status;
    protected $encoder;

    public function __construct(EncoderService $encoder)
    {
        $this->encoder = $encoder->getEncoder();
    }

    public function index(Request $request)
    {
        $data = [
            new MyModel(1, 'Some', "Attribute")
        ];

        return response($this->encoder->encodeData($data));
    }
}

可以配置额外的命名编码器,如下所示

<?php
return [
    'media-type' => 'application/vnd.api+json',
    'schemas' => [
        App\Models\MyModel::class => App\Schemas\MySchema::class,
    ],
    'jsonapi' => true,
    'encoder-options' => [
        'options' => JSON_PRETTY_PRINT,
    ],
    'meta' => [
        'apiVersion' => '1.1',
    ],
    'encoders' => [
        'custom' => [
            'jsonapi' => true,
            'encoder-options' => [
                'options' => JSON_PRETTY_PRINT,
            ],
            'meta' => [
                'apiVersion' => '2.0',
            ],
        ]
    ]
];

然后可以像这样检索

    $this->encoder = $encoder->getEncoder('custom');

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

测试

$ composer test

贡献

有关详细信息,请参阅 CONTRIBUTING

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件