twentytwo-labs / api-service-bundle
将API服务集成到Symfony
1.1.1
2024-09-23 18:29 UTC
Requires
- php: >=8.0
- ext-json: *
- nyholm/psr7: ^1.5
- php-http/httplug: ^2.4
- php-http/promise: ^1.2
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^2.0
- symfony/framework-bundle: ^5.0 || ^6.0 || ^7.0
- symfony/serializer: ^5.0 || ^6.0 || ^7.0
- twentytwo-labs/api-validator: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- infection/infection: ^0.28
- matthiasnoback/symfony-dependency-injection-test: ^5.0
- phpstan/phpstan: ^1.11
- phpstan/phpstan-deprecation-rules: ^1.2
- phpstan/phpstan-phpunit: ^1.4
- phpunit/phpunit: ^10.4
- squizlabs/php_codesniffer: ^3.4
- symfony/http-client: ^5.0 || ^6.0 || ^7.0
Suggests
- cache/adapter-bundle: Integrate cache pool providers in Symfony <3.1
- php-http/guzzle7-adapter: HttpClient adapter using Guzzle7
- php-http/httplug-bundle: Integrate HTTP Clients into Symfony
- symfony/http-client: Integrate HTTP Clients into Symfony
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缓存接口的实现。您不需要额外的组件来将缓存功能集成到框架中。