twentytwo-labs/api-service-bundle

将API服务集成到Symfony

安装: 60

依赖者: 1

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

1.1.1 2024-09-23 18:29 UTC

This package is auto-updated.

Last update: 2024-09-23 18:30:52 UTC


README

此扩展将API Service 组件集成到Symfony。

安装

打开命令行控制台,进入您的项目目录,并执行以下命令以下载此扩展的最新稳定版本

composer require twentytwo-labs/api-service-bundle

然后,通过在您的项目中的app/AppKernel.php文件中添加以下行来启用此扩展

<?php
// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new TwentytwoLabs\ApiServiceBundle\ApiServiceBundle(),
        );
        // ...
    }
}

配置

此扩展提供的完整配置

api_service:
  # (optional)
  default_services:
    client: httplug.client
    message_factory: httplug.message_factory
    uri_factory: httplug.uri_factory
  # (optional) cache schema files
  cache:
    # provide the Id of any PSR-6 cache service
    service: 'my_app.cache'
  # (optional) configure supported pagination providers
  pagination:
    # extract pagination from response headers
    header:
      page: X-Page
      perPage: X-Per-Page
      totalPages: X-Total-Pages
      totalItems: X-Total-Items
  # configure api services
  apis:
    my_service:
      # the schema describing your api
      schema: 'file://%kernel.root_dir%/config/schema/foo.yml'
      # (optional) use a specific http client implementation
      client: httplug.client.foo
      # (optional) fine tune your api service
      config:
        # provide a base url
        baseUri: https://bar.com
        # validate request
        validateRequest: true
        # validate response
        validateResponse: false
        # return a psr-7 response
        # by default it return a `Resource` class
        returnResponse: false

依赖项

HTTP客户端

Api Service 组件使用由HttPlug提供的Http\Client\HttpClient接口来发送请求。

您可以在Symfony中使用自己的HTTP客户端服务,但我们强烈建议您使用HttplugBundle

composer require php-http/httplug-bundle

然后,您可以选择由HTTPlug支持的许多HTTP客户端适配器之一。适配器列表在这里提供。

# for example, here we choose the Guzzle 6 adapter
composer require php-http/guzzle6-adapter

缓存

此扩展具有缓存您API服务使用的模式文件的能力。
它使用PSR-6: 缓存接口来实现。

出于性能考虑,缓存在生产环境中应该被启用。

从Symfony 4.0及以上版本

Symfony 3.1提供了PSR-6缓存接口的实现。您不需要额外的组件来将缓存功能集成到框架中。