marcelsud/shop-api-plugin

为Sylius电子商务平台提供Shop API。

安装: 1

依赖者: 0

建议者: 0

安全性: 0

星级: 0

关注者: 2

分支: 89

类型:sylius-plugin


README

该仓库在Sylius电子商务平台之上提供了一个ShopApi实现。

注意

它也是Sylius - 标准的补充。请检查官方文档以了解基本概念。

先决条件

为了运行此插件,您需要满足以下要求

  1. 已安装Composer

    $ wget https://getcomposer.org.cn/composer.phar
    $ php composer.phar create-project -s beta sylius/sylius-standard project
  2. 已安装Sylius

    $ cd project
    $ php bin/console sylius:install

其余命令都在project文件夹内执行。

用法

  1. 运行composer require sylius/shop-api-plugin

  2. 扩展配置文件

    1. 将SyliusShopApi添加到AppKernel。
    // app/AppKernel.php
    
        /**
         * {@inheritdoc}
         */
        public function registerBundles()
        {
            $bundles = [
                // ...
    
                new \Sylius\ShopApiPlugin\ShopApiPlugin(),
                new \League\Tactician\Bundle\TacticianBundle(),
            ];
    
            return array_merge(parent::registerBundles(), $bundles);
        }
    1. - { path: '^/shop-api', priorities: ['json'], fallback_format: json, prefer_extension: true }添加到app/config/config.yml文件中的fos_rest.format_listener.rules部分,并从插件导入配置。
    # app/config/config.yml
    
    imports:
        # ...
        - { resource: "@ShopApiPlugin/Resources/config/app/config.yml" }
    
    # ...
    
    fos_rest:
        # ...
        
        format_listener:
            rules:
                - { path: '^/shop-api', priorities: ['json'], fallback_format: json, prefer_extension: true } # <-- Add this
                - { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
                - { path: '^/', stop: true }
    
    1. 将路由添加到app/config/routing.yml
    # app/config/routing.yml
    
    # ...
    
    sylius_shop_api:
        resource: "@ShopApiPlugin/Resources/config/routing.yml"
    1. 配置防火墙
      1. sylius.security.shop_regex参数更改为排除shop-api前缀
      2. 添加ShopAPI正则表达式参数shop_api.security.regex: "^/shop-api"
      3. 添加ShopAPI防火墙配置
    parameters:
        # ...
    
        sylius.security.shop_regex: "^/(?!admin|api|shop-api)[^/]++" # shop-api has been added inside the brackets 
        shop_api.security.regex: "^/shop-api"
    
    # ... 
    
    security:
        firewalls:
            // ...
    
            shop_api:
                pattern: "%shop_api.security.regex%"
                stateless:  true
                anonymous:  true
    1. 调整结账配置以避免与Sylius商店API冲突。例如(假设您使用的是常规Sylius安全定义)
    # app/config/config.yml
    
    # ...
    
    sylius_shop:
        checkout_resolver:
            pattern: "%sylius.security.shop_regex%/checkout/"
    1. (可选)如果您已安装用于支持跨源Ajax请求的nelmio/NelmioCorsBundle

      1. 将NelmioCorsBundle添加到AppKernel
      // app/AppKernel.php
      
      /**
       * {@inheritdoc}
       */
      public function registerBundles()
      {
          $bundles = array(
              // ...
              new Nelmio\CorsBundle\NelmioCorsBundle(),
              // ...
          );
          // ...
      }
      1. 将配置添加到`config.yml
      # app/config/config.yml
      
      # ...
      
      nelmio_cors:
          defaults:
              allow_credentials: false
              allow_origin: []
              allow_headers: []
              allow_methods: []
              expose_headers: []
              max_age: 0
              hosts: []
              origin_regex: false
              forced_allow_origin_value: ~
          paths:
              '^/shop-api/':
                  allow_origin: ['*']
                  allow_headers: ['Content-Type', 'authorization']
                  allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS']
                  max_age: 3600

附加功能

属性

如果您希望接收序列化的属性,您需要在shop_api.included_attributes键下定义它们的代码数组。例如。

shop_api:
    included_attributes:
        - "MUG_MATERIAL_CODE"

授权

默认情况下,此包不提供授权。但它与LexikJWTAuthenticationBundle兼容。要检查示例配置,请查看

来自测试应用。

测试

可以使用API测试用例来测试应用程序。要运行测试套件,请执行以下命令

$ bin/phpunit