paloma/client-bundle

2.3.0 2019-04-17 11:58 UTC

This package is auto-updated.

Last update: 2024-09-17 23:17:28 UTC


README

这是一个用于将paloma/shop-client包轻松集成到Symfony应用程序中的Symfony包。

功能

  • 在Symfony应用程序配置中配置Paloma商店客户端
  • Paloma客户端工厂和便利服务定义
  • 集成到Symfony Web Profiler

用法

获取默认的Paloma客户端(请参阅以下内容了解这意味着什么)

$client = $container->get('paloma_client.default_client');

您可以通过使用DefaultPalomaClient类来利用Symfony的自动装配功能,而不是通过$cotainer->get()方法在运行时请求默认客户端

public function mySymfonyAction(DefaultPalomaClient $paloma) {
  $categories = $paloma->catalog()->categories();
}

获取客户端工厂以获取除默认客户端之外的客户端

$factory = $container->get('paloma_client.client_factory');
$defaultClient = $factory->getDefaultClient();
$myOtherChannelClient = $factory->getClient('my-other-channel', 'my-locale');

安装

将包添加到您的应用程序

composer require paloma/client-bundle

然后将它添加到您的AppKernel.php

$bundles = [
    [...]
    new Paloma\ClientBundle\PalomaClientBundle(),
    [...]
];

配置

paloma_client:
    base_url: 'https://my-api-endpoint'  # Probably get this from parameters
    api_key: MyApiKey  # Probably get this from parameters
    # The log format to use for Paloma requests which are deemed successful.
    # If not set the default specified in paloma/shop-client will be used.
    log_format_success: ~
    # The log format to use for Paloma requests which are deemed failed.
    # If not set the default specified in paloma/shop-client will be used.
    log_format_failure:  ~
    # The cache provider to use as the caching backend. Has to be a provider
    # which implements the PSR-6 CacheItemPoolInterface. Ideally one uses
    # the php-cache/adapter-bundle to define and configure a provider service. 
    cache_provider: ~

频道和区域设置

Paloma围绕频道的概念构建。一个Paloma配置可能包含许多不同的频道。频道的良好示例是“国家”或“租户”(在多租户系统中)。每个Paloma API端点都以前缀为频道(有关更多信息,请参阅Paloma API文档)。除了频道之外,大多数API端点也接受请求的区域设置前缀。Paloma本身无法确定正确的频道或区域设置,这由前端应用程序定义。

通常,这项工作是通过在前端应用程序中应用某些URL方案来完成的,例如:https://<tenant>.myclient.com/<country>/<language>/<path>。在这个例子中,结果可能是Paloma频道<tenant>_<country>和区域设置<language>。重要的是,频道是项目特定的,并且可以在语法和语义上发生变化。因此,必须在每个应用程序中再次解决。

解决此问题的标准方法是通过安装请求监听器,该监听器分析传入的URL并从该URL确定频道和区域设置。然后使用以下方法设置默认频道:

$factory = $container->get('paloma_client.client_factory');
$factory->setDefaultChannel('<my determined channel>');
$factory->setDefaultLocale('<my determined locale>');

在此之后,应用程序中的其他代码无需关心频道或区域设置。这应该可以处理大多数常见情况,因为在同一请求中需要处理多个频道或区域设置的需求很少。

Paloma跟踪ID

Paloma允许在每次请求中指定一个跟踪ID。这有助于关联Paloma和客户端应用程序中观察到的请求。

Paloma跟踪ID必须是一个长度为8个字符的字符串,仅由小写字母a-z和数字0-9组成。

可以在ClientFactory中设置此跟踪ID,以便将其添加到通过该工厂创建的客户端发送到Paloma的每个请求中。设置跟踪ID的便利位置可能是与默认频道和区域设置相同的区域。

可以这样做

$factory = $container->get('paloma_client.client_factory');
# This is just an example of how to specify a trace ID, one might use more
# elaborate approaches which include session and request information.
$factory->setPalomaTraceId(substr(uniqid(), 0, 8));