gimmenetwork/api-platform

构建API的统一工具

该包的规范存储库似乎已丢失,因此该包已被冻结。

0.0.9 2021-04-06 12:20 UTC

This package is auto-updated.

Last update: 2021-04-06 12:20:44 UTC


README

  1. 安装
  2. 配置

安装

非常简单

composer require gimmenetwork/api-platform

在安装了bundle之后,在 config/bundles.php 中启用它

<?php

return [
	...
    Gimmemore\API\ApiPlatformBundle::class => ['all' => true]
]

用法

过滤器

Kernel 中注册编译器传递

<?php

// src/Kernel.php

use Gimmemore\API\Filters\Filter;
use Gimmemore\API\DependencyInjection\CompilerPass\RegisterFilters;

...

protected function build(ContainerBuilder $container) : void
{
	...
    $container->registerForAutoconfiguration(Filter::class)->addTag(RegisterFilters::CONTAINTER_TAG);
    $container->addCompilerPass(new RegisterFilters());
}

每个过滤器实现 Filter 接口

<?php

declare(strict_types=1);

namespace Gimmemore\API\Filters;

use Doctrine\ORM\QueryBuilder;

interface Filter
{
    public function applyTo(QueryBuilder $builder, string $alias): void;

    public static function getType(): string;
    public static function createFromValue($value): self;
}

如果您想按 status 过滤,您必须注册具有 status 类型的 Filter。

异常处理

Kernel 中注册编译器传递

<?php

// src/Kernel.php

use Gimmemore\API\ExceptionHandling\ExceptionResponseHandler;
use Gimmemore\API\DependencyInjection\CompilerPass\RegisterResponseHandlers;

...

protected function build(ContainerBuilder $container) : void
{
	...
    $container->registerForAutoconfiguration(ExceptionResponseHandler::class)->addTag(RegisterResponseHandlers::CONTAINER_TAG);
    $container->addCompilerPass(new RegisterResponseHandlers());
}

每个 Exception 类只能有一个处理程序。